10

I've created 2 projects:

  1. Normal, basic ASP.NET MVC 4 application
  2. Basic ASP.NET WebAPI application

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);
    }
}

I registered it in global.asax in both:

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

I put a breakpoint in

return base.SendAsync(request, cancellationToken);

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?

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.

4

2 回答 2

8

正如下面ASP.NET Web API: HTTP Message LIFECYLE微软著名的图表所示,ASP.NET Web API 有一个额外的扩展点,称为Message Handlers(section HTTP Message Handlers)。

到目前为止,ASP.NET MVC(目前是 5 个)还没有提供这样的扩展点。

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

在此处输入图像描述

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

于 2016-03-10T00:15:30.553 回答
2

可以对比一下 MVC 和 WebAPI 的路由配置方式

对于 MVC

可用的重载是,

  public static class RouteCollectionExtensions
    {

        public static VirtualPathData GetVirtualPathForArea(this RouteCollection routes, RequestContext requestContext, RouteValueDictionary values);

        public static VirtualPathData GetVirtualPathForArea(this RouteCollection routes, RequestContext requestContext, string name, RouteValueDictionary values);

        public static void IgnoreRoute(this RouteCollection routes, string url);

        public static void IgnoreRoute(this RouteCollection routes, string url, object constraints);

        public static Route MapRoute(this RouteCollection routes, string name, string url);

        public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults);

        public static Route MapRoute(this RouteCollection routes, string name, string url, string[] namespaces);

        public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, object constraints);
       namespaces.

        public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, string[] namespaces);

        public static Route MapRoute(this RouteCollection routes, string name, string url, object defaults, object constraints, string[] namespaces);
    }

对于 WebAPI

    public static class HttpRouteCollectionExtensions
    {
        public static IHttpRoute MapHttpRoute(this HttpRouteCollection routes, string name, string routeTemplate);

        public static IHttpRoute MapHttpRoute(this HttpRouteCollection routes, string name, string routeTemplate, object defaults);

        public static IHttpRoute MapHttpRoute(this HttpRouteCollection routes, string name, string routeTemplate, object defaults, object constraints);

        public static IHttpRoute MapHttpRoute(this HttpRouteCollection routes, string name, string routeTemplate, object defaults, object constraints, HttpMessageHandler handler);

    }

看,最后一个webapi路由配置方法有一个参数,你可以传入你想要的自定义HttpMessageHandler。MVC 路由在其管道中没有该规定。

总而言之,MVC 执行上下文和管道与 WebAPI 完全不同,因为您的断点不会停留在您想要的位置。

希望有所帮助。

于 2014-02-25T07:09:20.757 回答