How does HelloWorld request URL instantiate HelloWorldController object in ASP.Net MVC?

I find this documentation very confusing.

After creating the file

using System.Web;
using System.Web.Mvc; 

namespace MvcMovie.Controllers 
{ 
    public class HelloWorldController : Controller 
    { 
        // 
        // GET: /HelloWorld/ 

        public string Index() 
        { 
            return "This is my <b>default</b> action..."; 
        } 

        // 
        // GET: /HelloWorld/Welcome/ 

        public string Welcome() 
        { 
            return "This is the Welcome action method..."; 
        } 
    } 
}

      

in the Controllers folder of my ASP.NET MVC project, I can make a request for a URL to a child page of HelloWorld

my site and apparently the object is instantiated HelloWorldController

and its method is called Index()

, How exactly this happens is a mystery to me. There is 1 sentence in the documentation that just says

The first part of the URL defines the controller class to execute. So / HelloWorld maps to the HelloWorldController class.

but I don't understand how it goes. There must be somewhere else in the MVC source files that take the url request for the subpage and add it to "Controller" and look for a class with the same name derived from the class Controller

. Where does this magic happen? Also, how does this translate at runtime, since at runtime the class names in the source code are dying out / out of date? Or am I completely confused about everything?

+3


source to share


2 answers


It is the responsibility of the DefaultControllerFactory to map the request from the routing engine to the controller .

DefaultControllerFactory

follows the conditional-config-config template . factory looks for a controller that meets the following criterion -



  • The class must be public
  • The class must be specific
  • The class should not accept a shared parameter
  • Class name must end with controller
  • The class must implement the IController interface

If you want to HelloWorld

request a map to a controller HelloWorldJonDoe

, you can create ControllerFactory

by overriding DefaultControllerFactory

.

+3


source


You are not completely confused. Routing requires a little learning curve for MVC. Take a look at your project at App_Start/RouteConfig.cs

. In this file, you will see where the default route is created for MVC.

public static void RegisterRoutes(RouteCollection routes)
{
   routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

   routes.MapRoute(
       name: "Default",
       url: "{controller}/{action}/{id}",
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
   );
}

      



The URL section shows what the URL will look like, and the defaults indicate what will be entered if you do not fill in all sections in the URL. This is how it renders the controller url segment. It does add Controller

in HelloWorld

to get the correct class, but that's why it knows HelloWorld

it's the controller in the first place.

I found this link helpful if you need more information on how to work with routes: https://msdn.microsoft.com/en-us/library/cc668201(v=vs.140).aspx

+2


source







All Articles