简体   繁体   English

为什么调用ASP.NET MVC Controller不执行DelegatingHandler?

[英]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. 我最近了解到,对ApiController Action的调用将触发DelegatingHandlerSendAsync方法,并且对vanilla Controller Action的调用不会触发它。

Search and Research 搜索和研究

I have looked into Web Api, and learned that it includes the HttpMessageHandler , which is the parent of the DelegatingHandler class. 我查看了Web Api,并了解到它包含了HttpMessageHandler ,它是DelegatingHandler类的父级。 That leads me to believe that HTTP Message Handlers, in general, only execute as part of a Web API pipeline. 这使我相信HTTP消息处理程序通常只作为Web API管道的一部分执行。

Also, Http Message Handlers run before Url Routing , so it probably isn't URL routing that chooses between the Web API and MVC pipeline. 此外, Http Message Handlers在Url Routing之前运行 ,因此它可能不是在Web API和MVC管道之间选择的URL路由。

...consider using an action filter instead of a message handler [because a]ction filters run after URI routing is performed. ...考虑使用动作过滤器而不是消息处理程序[因为a]过滤器在执行URI路由后运行。

Question

  • Are HttpMessageHandlers a part of ASP.NET Web API but not of ASP.NET MVC? HttpMessageHandlers是ASP.NET Web API的一部分,而不是ASP.NET MVC的一部分吗?
  • What is the HttpMessageHandler equivalent in MVC ( EQUIVALENT in the diagram)? 什么是MVC中的HttpMessageHandler等价物(图中的EQUIVALENT )?
  • What causes the request to follow the Web API pipeline ( FORK in the diagram)? 是什么导致请求遵循Web API管道(图中的FORK )?
  • Are Web API requests fundamentally different that MVC requests? Web API请求是否与MVC请求根本不同?

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应用程序生命周期

ASP.NET Web API Poster ASP.NET Web API海报

Life Cycle of an ASP.NET MVC 5 Application ASP.NET MVC 5应用程序的生命周期

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. 好吧,因为它们运行两个不同的执行路径,如果MVC路由匹配它然后执行MVCHandler,而委托处理程序仅在Api路由匹配时执行。 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. 路由是将url与一组字符串段匹配以生成RouteValues的步骤,该RouteValues将路由键映射到路由值。 RouteValues is then used in action selection. 然后在行动选择中使用RouteValues 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. 在MVC中没有用于委派处理程序的等效方法,类似的方法是编写自己的处理程序,但是你在那里深入了解,尤其是链接生成。

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? HttpMessageHandlers是ASP.NET Web API的一部分,而不是ASP.NET MVC的一部分吗?

Yes they are only WebAPI constructs. 是的,它们只是WebAPI构造。

What is the HttpMessageHandler equivalent in MVC (EQUIVALENT in the diagram)? 什么是MVC中的HttpMessageHandler等价物(图中的EQUIVALENT)?

None really exist, and the diagram is wrong. 没有确实存在,图表是错误的。 The closest is a RouteHandler 最接近的是RouteHandler

What causes the request to follow the Web API pipeline (FORK in the diagram)? 是什么导致请求遵循Web API管道(图中的FORK)?

Matching a WebAPI route 匹配WebAPI路由

Are Web API requests fundamentally different that MVC requests? Web API请求是否与MVC请求根本不同?

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. 编辑(2015年8月18日):在MVC Core中,Web API被合并到MVC中,并且有一个新的管道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM