ASP.NET MVC Message Handlers vs Web API Message Handlers

前端 未结 2 725
说谎
说谎 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: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.

提交回复
热议问题