ASP.NET MVC Message Handlers vs Web API Message Handlers

前端 未结 2 724
说谎
说谎 2021-01-17 16:02

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 mes

相关标签:
2条回答
  • 2021-01-17 16:43

    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).

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

    [Link to the full size PDF poster]

    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.

    0 讨论(0)
  • 2021-01-17 16:55

    You can compare the route configuration methods of both MVC and WebAPI

    For MVC

    The available overloads are,

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

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

    See, the last webapi route configuration method has a parameter where you can pass customized HttpMessageHandler you want. MVC routing does not have that provision in its pipeline.

    In summation, the MVC execution context and pipeline are totally different from WebAPI due to that fact your break point does not sticks where you want.

    Hope helps.

    0 讨论(0)
提交回复
热议问题