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
HttpMessageHandlers
a part of ASP.NET Web API but not of ASP.NET MVC?HttpMessageHandler
equivalent in MVC (EQUIVALENT in the diagram)?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
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.
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.
You can check the Request. HttpMethod property.
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.
Yes they are only WebAPI constructs.
None really exist, and the diagram is wrong. The closest is a RouteHandler
Matching a WebAPI route
No they are not, the fork happens only after routing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With