Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does a call to an ASP.NET MVC Controller not execute a DelegatingHandler?

Introduce the Question

I have recently learned that a call to an ApiController Action will trigger a DelegatingHandler's SendAsync method, and that a call to a vanilla Controller Action will not trigger it.

Search and Research

I have looked into Web Api, and learned that it includes the HttpMessageHandler, which is the parent of the DelegatingHandler class. That leads me to believe that HTTP Message Handlers, in general, only execute as part of a Web API pipeline.

Also, Http Message Handlers run before Url Routing, so it probably isn't URL routing that chooses between the Web API and MVC pipeline.

...consider using an action filter instead of a message handler [because a]ction filters run after URI routing is performed.

Question

  • Are HttpMessageHandlers a part of ASP.NET Web API but not of ASP.NET MVC?
  • What is the HttpMessageHandler equivalent in MVC (EQUIVALENT in the diagram)?
  • What causes the request to follow the Web API pipeline (FORK in the diagram)?
  • Are Web API requests fundamentally different that MVC requests?

My sense is that it goes something like this, but please correct me.

               Request from Client                        |                       IIS                        |                                                ASP.NET                        |             HttpApplication.BeginRequest                        |                    et cetera                        |          HttpApplication.MapRequestHandler - is this what does the routing?                        |                      FORK                                               /       \                   /         \                  /           \                 /             \                /               \          **Web API**           **MVC**               |                   |  HttpControllerRouteHandler   MvcRouteHandler               |                   |     HttpControllerHandler         |                   |                   |     HttpMessageHandlers       EQUIVALENT?             i.e.                  |     DelegatingHandlers            |             incl.                 |     HttpServer                    |     CustomHandlers                |     HttpRoutingDispatcher         |     HttpControllerDispatcher      | 

Helpful Links

ASP.NET Application Lifecycle

ASP.NET Web API Poster

Life Cycle of an ASP.NET MVC 5 Application

like image 527
Shaun Luttin Avatar asked May 07 '14 01:05

Shaun Luttin


People also ask

What is the usage of Delegatinghandler?

Typically, a series of message handlers are chained together. The first handler receives an HTTP request, does some processing, and gives the request to the next handler. At some point, the response is created and goes back up the chain. This pattern is called a delegating handler.

Which action is invoked when we call any controller in MVC?

Index() action is invoked, a view is not returned. Instead, the raw text "Hello World!" is returned to the browser. If a controller action returns a result that is not an action result - for example, a date or an integer - then the result is wrapped in a ContentResult automatically.

How can we detect that an MVC controller is called by post or get?

You can check the Request. HttpMethod property.


1 Answers

Why does a call to a controller does not execute a delegating handler?

Well because they run down two different execution paths, if an MVC route matches it then executes an MVCHandler, while a delegating handler only executes when an Api route matches. In short the diagram above doesn't describe the split correctly.

Delegating handlers run AFTER routing and before action selection. The routing and action selection steps get often confused or used interchangeably although they are two distinct steps.

Routing is the step that matches a url to a set of string segments to produce the RouteValues which maps a route key to a route value. RouteValues is then used in action selection. The delegating handlers run in between these two steps.

There is no equivalent in MVC for delegating handlers, a similar way to do it is to write your own handler but you are getting in some deep water there, particularly with link generation.

Another simpler approach is to write a global filter, but note that it will only run if an action was actually selected.

Line by line answers

Are HttpMessageHandlers a part of ASP.NET Web API but not of ASP.NET MVC?

Yes they are only WebAPI constructs.

What is the HttpMessageHandler equivalent in MVC (EQUIVALENT in the diagram)?

None really exist, and the diagram is wrong. The closest is a RouteHandler

What causes the request to follow the Web API pipeline (FORK in the diagram)?

Matching a WebAPI route

Are Web API requests fundamentally different that MVC requests?

No they are not, the fork happens only after routing.

EDIT (Aug-18-2015): In MVC Core, Web API was merged into MVC, and there is a single new pipeline.

like image 183
Yishai Galatzer Avatar answered Oct 02 '22 15:10

Yishai Galatzer