简体   繁体   中英

Custom message handler with attribute based routing is not working for Web API 2

I have a Message Handler in my Web API 2 project which change the url to a different path. Such as url "root/controller/somevalue/detials" will be changed to "root/controller/2/details" . Where "somevalue" or 2 is a parameter for the action method.

I have placed the message handler registration code before calling config.MapHttpAttributeRoutes(); Message handler is registered as config.MessageHandlers.Add(new MyHandler());

For normal route mapping it is working fine. But if I enable attribute based routing then the message handler is called and changed the url but action method is hit with the same "somevalue" not "2".

What is happening here?

If you are using RouteAttribute , then you can have Route value like this for capturing somevalue :

Route["details/{somevalue}"]
public IHttpActionResult MyCustiomApiMethod(string somevalue)
{
    //return 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM