asp.net-mvc asp.net mvc 4 应用程序的入口点是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16922742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:54:07  来源:igfitidea点击:

what is the entry point for an asp.net mvc 4 application?

asp.net-mvc

提问by bernie2436

I am learning my way around an MVC 4 application but am new to the mvc platform. If I were learning a new java application, I would start reading code from the main method and go from there. What is the analog for an asp.net mvc application?

我正在学习 MVC 4 应用程序,但我是 mvc 平台的新手。如果我正在学习一个新的 Java 应用程序,我会从 main 方法开始阅读代码,然后从那里开始。asp.net mvc 应用程序的模拟是什么?

I launch the application from the following URL: http://server/directory/home?iv-user=tuser

我从以下 URL 启动应用程序: http://server/directory/home?iv-user=tuser

采纳答案by p.campbell

It's HTTP. You make a request to the web server for a resource, as you have specified above, and the controller responds.

它是 HTTP。如上所述,您向 Web 服务器请求资源,然后控制器响应。

So in ASP.NET MVC, you have multiple entry points: each action method.

因此,在 ASP.NET MVC 中,您有多个入口点:每个操作方法。

MSDN Controllers and Action Methods in ASP.NET MVC

ASP.NET MVC 中的 MSDN 控制器和操作方法

回答by MaxWillmo

The Global.asax.csfile, where there is the start method Application_Startmight be what you are looking for. That is the code which is run when the app starts.

Global.asax.cs包含 start 方法的文件Application_Start可能就是您要查找的内容。这是应用程序启动时运行的代码。

protected void Application_Start()
{
    ...
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    ...
}

But looking at the url you have posted it might be the HomeControlleror DirectoryControllerfile. Unfortunately, I cannot tell that from looking at your route.

但是查看您发布的网址,它可能是HomeControllerorDirectoryController文件。不幸的是,我无法通过查看您的路线来判断这一点。

A sample route register code is as below where we can see that

示例路由注册代码如下所示,我们可以看到

  1. The URL /{controller}/{action}/{id}

  2. The default for controller/action/id is Home/Index/optional

  1. URL /{controller}/{action}/{id}

  2. controller/action/id 的默认值是 Home/Index/optional

So if you run your web with the starting url as http://localhost:52763/, it is indeed will call http://localhost:52763/Home/Index

因此,如果您使用起始 url as 运行您的网络http://localhost:52763/,它确实会调用http://localhost:52763/Home/Index

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 }
    );
}

回答by Tom Chantler

In the case you have given, if you want to find the first bit of non-framework code which is executed, you would look for HomeController.csin the Controllersdirectory and look for the Indexor Defaultmethod which will accept a parameter of iv-user.

在这种情况下,你给了,如果你想找到被执行的非框架代码第一位,你能看到HomeController.csControllers目录,并寻找IndexDefault将接受的参数方法iv-user

To see the full page lifecycle, look here: http://www.red-gate.com/products/dotnet-development/ants-performance-profiler/entrypage/be-ahead-of-the-game-poster2. This takes you from the incoming HTTP request to the final rendered view.

要查看完整的页面生命周期,请查看此处:http: //www.red-gate.com/products/dotnet-development/ants-performance-profiler/entrypage/be-ahead-of-the-game-poster2。这会将您从传入的 HTTP 请求带到最终呈现的视图。

回答by Gabe

There is an application_start()method in global.asax.cs. As for the controller's concern for the request, its starts in the constructor of the controller then the method for the requested action.

application_start()global.asax.cs 中有一个方法。至于控制器对请求的关注,它从控制器的构造函数开始,然后是请求操作的方法。

回答by Trong-Hieu Tran

MR P.Campbell was right. For more detail please review as: http://msdn.microsoft.com/en-us/library/dd381612(v=vs.98).aspx

P.Campbell 先生是对的。有关更多详细信息,请查看:http: //msdn.microsoft.com/en-us/library/dd381612(v=vs.98).aspx

I copied it's main content:

我复制了它的主要内容:

Understanding MVC Application Execution

了解 MVC 应用程序执行

Requests to an ASP.NET MVC-based Web application first pass through the UrlRoutingModule object, which is an HTTP module. This module parses the request and performs route selection. The UrlRoutingModule object selects the first route object that matches the current request. (A route object is a class that implements RouteBase, and is typically an instance of the Route class.) If no routes match, the UrlRoutingModule object does nothing and lets the request fall back to the regular ASP.NET or IIS request processing. From the selected Route object, the UrlRoutingModule object obtains an object that implements the IRouteHandler interface and that is associated with the Route object. Typically, in an MVC application, this will be an instance of the MvcRouteHandler class. The MvcRouteHandler instance creates an MvcHandler object that implements the IHttpHandler interface. The MvcHandler object then selects the controller that will ultimately handle the request. For more information, see ASP.NET Routing.

对基于 ASP.NET MVC 的 Web 应用程序的请求首先通过 UrlRoutingModule 对象,它是一个 HTTP 模块。该模块解析请求并执行路由选择。UrlRoutingModule 对象选择与当前请求匹配的第一个路由对象。(路由对象是实现 RouteBase 的类,通常是 Route 类的实例。)如果没有路由匹配,则 UrlRoutingModule 对象不执行任何操作,让请求回退到常规 ASP.NET 或 IIS 请求处理。UrlRoutingModule 对象从选定的 Route 对象获取一个实现 IRouteHandler 接口并与 Route 对象关联的对象。通常,在 MVC 应用程序中,这将是 MvcRouteHandler 类的实例。MvcRouteHandler 实例创建一个实现 IHttpHandler 接口的 MvcHandler 对象。MvcHandler 对象然后选择最终处理请求的控制器。有关详细信息,请参阅 ASP.NET 路由。

回答by Minoosha

The Starting point is the public static void Main(string[] args) {...} in program.cs file.

起点是 program.cs 文件中的 public static void Main(string[] args) {...}。