Why is my Web API method with double args not getting called?

瘦欲@ 提交于 2019-11-27 15:39:00
Joel

I was able to reproduce this on my machine.

Simply adding a / to the end of the URL corrected it for me. Looks like the routing engine is viewing it as a .99 file extension, rather than an input parameter.

http://localhost:28642/api/inventoryitems/GetDeptRange/1.1/99.99/

Additionally, it looks like you can register a custom route that automatically adds a trailing slash to the end of the URL when using the built-in helpers to generate the link. I have not tested this personally: stackoverflow Add a trailing slash at the end of each url

The easiest solution is to just add the following line to your RouteCollection. Not sure how you would do it with the attribute, but in your RouteConfig, you just add this:

routes.AppendTrailingSlash = true;

As noted by Joel, this is probably IIS picking up on the file extension and trying to serve a static file. You can get around this by adding the following into your web.config file (under system.webServer):

<modules>
    <remove name="UrlRoutingModule-4.0" />
    <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule"
               preCondition="" />
</modules>

By default, IIS will only run this module for what it deems to be requests for ASP.NET resources - the above removes this condition on a per-site basis, allowing you to route all requests through ASP.NET MVC/Web API routing.

Static files will still be preferred if they exist, so this shouldn't cause any issues elsewhere.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!