请求URL“HelloWorld”如何在ASP.Net MVC中实例化HelloWorldController对象?

时间:2021-05-18 03:34:56

I'm finding this documentation very confusing.

我发现这个文档非常令人困惑。

After creating a 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 URL request to the child page HelloWorld of my site and apparently an instance of a HelloWorldController object is created and its method Index() invoked. How exactly that happens is a mystery to me. The documentation has 1 sentence that simply says

在我的ASP.NET MVC项目的Controllers文件夹中,我可以向我的站点的子页面HelloWorld发出URL请求,显然创建了一个HelloWorldController对象的实例,并调用了它的方法Index()。究竟是怎么回事对我来说是一个谜。该文档有一句简单的说

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

URL的第一部分确定要执行的控制器类。所以/ HelloWorld映射到HelloWorldController类。

but I don't understand how that happens. There must be somewhere else in the MVC source files that takes a URL request for a subpage and appends it with "Controller" and looks for a class with the same name derived from the Controller class. Where does that magic happen? Also, how does that translate into runtime, since at runtime the names of classes in the source code are extinct/irrelevant? Or am I totally confused about everything?

但我不明白这是怎么回事。 MVC源文件中必须有其他地方接受子页面的URL请求,并将其附加到“Controller”并查找从Controller类派生的具有相同名称的类。那魔法在哪里发生?另外,这是如何转换为运行时的,因为在运行时源代码中的类名称已经灭绝/不相关?或者我对一切感到困惑?

2 个解决方案

#1


Mapping a request from the routing system to a controller is the responsibility of DefaultControllerFactory class.

将请求从路由系统映射到控制器是DefaultControllerFactory类的责任。

DefaultControllerFactory follows the convention-over-configuration pattern. The factory looks for controller which meets the following critera -

DefaultControllerFactory遵循约定配置模式。工厂寻找满足以下标准的控制器 -

  • The class must be public
  • 这堂课必须公开

  • The class must be concrete
  • 这门课必须具体

  • The class must not take generic parameter
  • 该类不能采用泛型参数

  • The name of the class must end with Controller
  • 类的名称必须以Controller结尾

  • The class must implement the IController interface
  • 该类必须实现IController接口

If you want HelloWorld request map to HelloWorldJonDoe controller, you can create ControllerFactory by overriding DefaultControllerFactory.

如果您希望HelloWorld请求映射到HelloWorldJonDoe控制器,您可以通过重写DefaultControllerFactory来创建ControllerFactory。

#2


You aren't totally confused about it. Routing takes a bit of learning for MVC. Take a look in your project for App_Start/RouteConfig.cs. In this file you will see where the default route is created for MVC.

你并不完全对此感到困惑。路由需要对MVC进行一些学习。在项目中查看App_Start / RouteConfig.cs。在此文件中,您将看到为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 passed in will look like and the defaults show what will be put in if you do not fill in all the sections in the URL. This is how it maps the URL segment to controller. It does essentially append Controller to HelloWorld to get the proper class, but this is why it knows that HelloWorld is the controller in the first place.

URL部分显示传入的URL将如何显示,默认值显示如果您未填写URL中的所有部分将放入的内容。这是它将URL段映射到控制器的方式。它基本上将Controller附加到HelloWorld以获得正确的类,但这就是为什么它首先知道HelloWorld是控制器的原因。

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

如果您需要有关如何使用路线的更多信息,我发现此链接很有用:https://msdn.microsoft.com/en-us/library/cc668201(v = vs.140).aspx

#1


Mapping a request from the routing system to a controller is the responsibility of DefaultControllerFactory class.

将请求从路由系统映射到控制器是DefaultControllerFactory类的责任。

DefaultControllerFactory follows the convention-over-configuration pattern. The factory looks for controller which meets the following critera -

DefaultControllerFactory遵循约定配置模式。工厂寻找满足以下标准的控制器 -

  • The class must be public
  • 这堂课必须公开

  • The class must be concrete
  • 这门课必须具体

  • The class must not take generic parameter
  • 该类不能采用泛型参数

  • The name of the class must end with Controller
  • 类的名称必须以Controller结尾

  • The class must implement the IController interface
  • 该类必须实现IController接口

If you want HelloWorld request map to HelloWorldJonDoe controller, you can create ControllerFactory by overriding DefaultControllerFactory.

如果您希望HelloWorld请求映射到HelloWorldJonDoe控制器,您可以通过重写DefaultControllerFactory来创建ControllerFactory。

#2


You aren't totally confused about it. Routing takes a bit of learning for MVC. Take a look in your project for App_Start/RouteConfig.cs. In this file you will see where the default route is created for MVC.

你并不完全对此感到困惑。路由需要对MVC进行一些学习。在项目中查看App_Start / RouteConfig.cs。在此文件中,您将看到为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 passed in will look like and the defaults show what will be put in if you do not fill in all the sections in the URL. This is how it maps the URL segment to controller. It does essentially append Controller to HelloWorld to get the proper class, but this is why it knows that HelloWorld is the controller in the first place.

URL部分显示传入的URL将如何显示,默认值显示如果您未填写URL中的所有部分将放入的内容。这是它将URL段映射到控制器的方式。它基本上将Controller附加到HelloWorld以获得正确的类,但这就是为什么它首先知道HelloWorld是控制器的原因。

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

如果您需要有关如何使用路线的更多信息,我发现此链接很有用:https://msdn.microsoft.com/en-us/library/cc668201(v = vs.140).aspx