11

Does anyone know whether message handler can work simultaneously with attribute routing in Web API 2.x? I got a custom message handler to work using conventional routing, then after adding attribute routing, it stops working. I am not sure whether it's not supported or if I had misconfigured something. Any help is greatly appreciated.

1) before attribute routing

--- WebApiConfig.cs  code snippet (simplified)----


        config.Routes.MapHttpRoute(
            name:"DefaultApi",
            routeTemplate: "api/{controller}",
            defaults: null,
            constraints: null,
            handler: my-message-handler-object
        );


--- MyController.cs  code snippet (simplified)----

    public class MyController : ApiController
    {

        [HttpGet]
        public IHttpActionResult CheckInServices(...)
         {
           ...
         }
    }

2) after attribute routing

--- WebApiConfig.cs  code snippet (simplified)----

    public static void Register(HttpConfiguration config)
    {
        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name:"DefaultApi",
            routeTemplate:"api/vendor",  
            defaults: new { controller = "Users" },
            constraints: null,
            handler: my-message-handler-object
        );
   }


--- MyController.cs  code snippet (simplified)----

    [RoutePrefix("api/vendor/{vendorID:long}/service")]
    public class MyController : ApiController
    {
         [HttpPost]
         [Route("{serviceID:long}")]
         public IHttpActionResult CheckInServices(...)
         {
           ...
         }
    }

Thanks,

Cody

cng
  • 111
  • 5

1 Answers1

0

Global message handlers will work - just set it up on start up.

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MessageHandlers.Add(new YourAuthenticationHandler());
    }
}

I'm unsure if per route Message Handlers work with Attribute Routing.

Richie
  • 505
  • 4
  • 13