How does IIS server identify that request is mvc request?
Asked Answered
I

3

6

In asp.net life cycle, on basis of the extension (.aspx), the request would be identified and handled by aspnet_isapi.dll and then httpapplication object is created followed by request and response objects and then request is processed by ProcessRequest() method.

I was going through mvc page life cycle

I have doubt about how the IIS server is able to identify the incoming request is MVC request?

Imaret answered 5/8, 2014 at 8:44 Comment(0)
I
4

Both the answers with some research I did, collectively resolves my question.

Step 1: From the below mentioned article #1 (which I found during my research):

From a very high view, IIS is just a process which is listening on a particular port (usually 80). Listening means it is ready to accept a connections from clients on port 80. A very important thing to remember is: IIS is not ASP.NET. This means that IIS doesn't know anything about ASP.NET; it can work by itself.

Step 2:

Note: When we deploy and start the application in IIS, it would call Application_Start which would register the routes. So, when MVC request comes to IIS, we are ready with our Route Table to handle this request.

Step 3:

As mentioned by @Babin, IIS doesn't know how to handle the request but because of ASP.NET framework, the request automatically goes to managed handlers.

Step 4:

As mentioned by @Rune, the request is intercepted by the UrlRoutingModule which in turn gets object of MvcRouteHandler class that will ultimately map to controller and action to handle the request.

Step 5:

As mentioned in one of SO question's comments:

If no routes match, the UrlRoutingModule object does nothing 
and lets the request fall back to the regular ASP.NET or IIS request processing.

References:

I found good articles to read through and clear out the doubts in IIS request processing.

1) The following link has in-depth explanation for how IIS handles ASP.NET WebForms request: http://www.codeproject.com/Articles/121096/Web-Server-and-ASP-NET-Application-Life-Cycle-in-D

2) The following links explains how IIS handles MVC requests using managed handlers: http://blogs.msdn.com/b/tmarq/archive/2010/04/01/asp-net-4-0-enables-routing-of-extensionless-urls-without-impacting-static-requests.aspx

3) MVC Lifecycle: http://pratiktips.blogspot.de/2012/08/the-magic-of-aspnet-mvc-model-binding.html

Imaret answered 2/4, 2015 at 11:51 Comment(1)
Got 1 more MSDN article explaining MVC Application Execution: msdn.microsoft.com/en-us/library/dd381612(v=vs.98).aspxImaret
C
2

IIS 7+ can run in two pipeline modes: "Classic mode" and "Integrated mode". The latter mode means that ASP.NET sees all incoming requests and can handle / manipulate them.

If you are asking how ASP.NET knows to invoke MVC, that is described in step 4 of the diagram you linked to: The UrlRoutingModule matches the request against all registered routes. When using MVC, you will have registered a route with a MvcRouteHandler. From MSDN:

A MvcRouteHandler instance is registered with routing when you use the MapRoute method. When the MvcRouteHandler class is invoked, the class generates an MvcHandler instance using the current RequestContext instance. It then delegates control to the new MvcHandler instance

Cunaxa answered 5/8, 2014 at 8:51 Comment(3)
I am asking about step 1 where IIS identifies that request needs to be processed ASP.NET engine. How does it do that?Imaret
By combining your answer with the @babin answer, I understand that II7+ has integrated mode which allows all incoming requests and asp.net would then process the request as per HttpHandler selected. Is my understanding correct?Imaret
@Cunaxa I want to make sure I understand: So by setting IIS to run in "Integrated Mode", essentially you're telling IIS to "inspect" every single reuqest it receives. It looks at "/some/request" and tries to determine if there is a some directory containing the request file. If that dir/file does NOT exist, then it checks to see if there is a some controller which defines the method request and invokes it?Moen
O
1

IIS doesn't know; ASP.NET knows via HTTP Handlers

Both WebForms and MVC are built on top of ASP.NET, and both use HTTP Handlers to deal with per-request execution:

  • WebForms has .aspx files mapped to the PageHandlerFactory: PageHandlerFactory implements IHttpHandlerFactory::GetHandler() returns HttpHandler

  • MVC integrates into the Routing infrastructure as an IRouteHandler implementation. Routes is notified of requests via the UrlRoutingHandler URLRoutingHanlder implements IHttpHandler. ASP.NET MVC is just a custom handler added to the ASP.NET pipeline.

Below is MVC 4.0 & ASP.NET 4.0 onwards These rules can be defined at any level in IIS. Most MVC applications define the handlers at the application level in the web.config file

<handler>
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit"/>
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit"/>
<add name="ExtensionlessUrlHandler-Integrated-4.0"/>
</handlers>
Odoriferous answered 19/1, 2015 at 23:53 Comment(1)
Does that mean without identifying the request, IIS forwards it to aspnet_isapi.dll to start the HTTP pipeline and lets asp.net decide how to process this request in Application_Start where routes are defined?Imaret

© 2022 - 2024 — McMap. All rights reserved.