mvc 4應用程序的切入點是什么?

[英]what is the entry point for an mvc 4 application?

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 mvc application?

我正在學習MVC 4應用程序,但我是mvc平台的新手。如果我正在學習一個新的java應用程序,我將開始從main方法中讀取代碼並從那里開始。 mvc應用程序的模擬是什么?

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

我從以下URL啟動應用程序:http:// server / directory / home?iv-user = tuser

6 个解决方案



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


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




The file, where there is the start method Application_Start, might be what you are looking for. That is the code which is run when the app starts.


protected void Application_Start()

But looking at the url you have posted it might be the HomeController or DirectoryController file. Unfortunately I cannot tell from looking at your route.


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


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

    網址/ {controller} / {action} / {id}

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

    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作為http:// localhost:52763 /運行您的網站,它確實會調用http:// localhost:52763 / Home / Index

public static void RegisterRoutes(RouteCollection routes)

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



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.




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.cs in the Controllers directory and look for the Index or Default method which will accept a parameter of iv-user.


To see the full page lifecycle, look here: This takes you from the incoming HTTP request to the final rendered view.




MR P.Campbell was right. For more detail please review as:

MR P.Campbell先生是對的。有關更多詳細信息,請查看: = vs.98).aspx

I copied it's main content:


Understanding MVC Application Execution


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請求處理。從選定的Route對象中,UrlRoutingModule對象獲取一個實現IRouteHandler接口並與Route對象關聯的對象。通常,在MVC應用程序中,這將是MvcRouteHandler類的實例。 MvcRouteHandler實例創建一個實現IHttpHandler接口的MvcHandler對象。然后,MvcHandler對象選擇最終將處理請求的控制器。有關更多信息,請參閱ASP.NET路由。



Here this link gives very detailed explanation of overall execution flow in MVC along with it's life-cycle events. A-Detailed-Walkthrough-of-ASP-NET-MVC-Request-Life.

這個鏈接給出了MVC中整體執行流程及其生命周期事件的非常詳細的說明。 A-詳細-演練-的-ASP-NET-MVC請求生命。

For deeper details follow this link. Asp-Net-mvc-life-cycle

有關詳細信息,請點擊此鏈接。 ASP-NET-MVC-生命周期



粤ICP备14056181号  © 2014-2021