0

I've created a DelegatingHandler and it works for every request. I've read some articles that i can define a handler for a specific route but I've tried a lot of ways without success, i'm missing something that i didn't found what is.

The last attempt was:

GlobalConfiguration.Configuration.Routes.MapHttpRoute(
    name: "SaveMessage",
    routeTemplate: "api/message/{type}",
    defaults: new { type = RouteParameter.Optional },
    constraints: null,
    handler: HttpClientFactory.CreatePipeline(
              new HttpControllerDispatcher(GlobalConfiguration.Configuration), 
              new DelegatingHandler[]{new ValidationHandler()})
);

My class Controller and method has the follow attributes:

[RoutePrefix("api/message")]
public class MessageController : ApiController
{
    [Route("{type}")]
    [HttpPost]
    public HttpResponseMessage Save(string type, [FromBody] Message message)
    {
        ....
    }
}

What is missing to my handler only works on requests like api/message/text?

I've read links like How to make more MapHttpRoutes for MVC 4 Api and http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-and-action-selection.

Community
  • 1
  • 1
  • So are you saying your delegatingHandler works on api/message/text routes? I think you have added the Handler to that route so that's how it should work then.. – Aram Jun 10 '15 at 17:35
  • No, it only works if i do something like **GlobalConfiguration.Configuration.MessageHandlers.Add(new ValidationHandler())** but every request hits the handler and i would like **only** request api/message/text hit the handler. –  Jun 10 '15 at 17:36

1 Answers1

0

The issue is the constraints that is set to null. You should define it like this for making it work for POST or GET:

constraints: new { httpMethod = new System.Web.Http.Routing.HttpMethodConstraint(HttpMethod.Post) }
Aram
  • 5,537
  • 2
  • 30
  • 41
  • If i have a query string like api/message/text?token=XXXX, should i add it to route config? –  Jun 10 '15 at 17:43
  • No, Querystrings are not part of the route – Aram Jun 10 '15 at 17:44
  • Here is what my browser submit "http://local.mycompany.com.ar/IntegrationApi/api/message/text?token=a5e56f178". I've tried to add IntegrationApi too but no lucky. –  Jun 10 '15 at 17:46