ASP.NET MVC消息处理程序与Web API消息处理程序 [英] ASP.NET MVC Message Handlers vs Web API Message Handlers

查看:63
本文介绍了ASP.NET MVC消息处理程序与Web API消息处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了2个项目:

I've created 2 projects:

  1. 普通的基本ASP.NET MVC 4应用程序
  2. 基本的ASP.NET WebAPI应用程序

我所做的是我添加了从DelegatingHandler派生的自定义消息处理程序.在这里:

What I did is I added my custom message handler, derived from DelegatingHandler to both of them. Here it is:

public class MyHandler : DelegatingHandler
{
    protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, System.Threading.CancellationToken cancellationToken)
    {
        return base.SendAsync(request, cancellationToken);
    }
}

我同时在global.asax中将其注册:

GlobalConfiguration.Configuration.MessageHandlers.Add(new MyHandler());

我在

return base.SendAsync(request, cancellationToken);

ASP.NET MVC和ASP.NET WebAPI之间的区别在于,当我调用ASP.NET MVC应用程序(http://localhost:4189/Something)时,不会触发断点.但是,当我调用Web API(http://localhost:7120/api/values)时,会触发断点 .

The difference between ASP.NET MVC and ASP.NET WebAPI is that when I call ASP.NET MVC application (http://localhost:4189/Something) the breakpoint is not triggered. When I call Web API however (http://localhost:7120/api/values), the breakpoint is triggered.

那是为什么?这些应用程序类型的执行流程是否有所不同?

Why is that? Are there any differences in those application types execution flows?

此外,当我尝试请求普通的Controller而不是像http://localhost:7120/Home这样的WebAPI应用程序的ApiController时,不会触发断点 .

In addition, when I try to request normal Controller, not ApiController of WebAPI application, like http://localhost:7120/Home, the break point is not triggered.

推荐答案

如下面著名的Microsoft ASP.NET Web API: HTTP Message LIFECYLE图所示,ASP.NET Web API具有一个称为Message Handlers的附加扩展点().

As shown below in the famous ASP.NET Web API: HTTP Message LIFECYLE diagram by Microsoft, the ASP.NET Web API has an additional extensibility point known as Message Handlers (section HTTP Message Handlers).

到目前为止,ASP.NET MVC(当前为5)尚未提供此类可扩展点.

So far ASP.NET MVC (5 currently) doesn't provide such extensibility point.

[链接到完整尺寸的PDF海报]

MVC和WebAPI共享(或至少在概念上)共享的唯一可扩展点是筛选器.请注意,MVC和WebAPI具有不同的注册过滤器的方式,这使许多人与您现在所处的情况相似.

The only extensibility point that the MVC and WebAPI share (or at least conceptually) are Filters. Beware that MVC and WebAPI have different ways to register filters and this puts many people in a similar situation as the one you are in right now.

这篇关于ASP.NET MVC消息处理程序与Web API消息处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆