ASP.NET MVC 4 Web API fails to map path containing the string “con”?

北城余情 提交于 2019-12-29 06:47:14

问题


What is so wrong about the string "con"?

OK, my api route configuration is rather uninteresting:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

The LocationController has the following method:

public List<LocationViewModel> Get(string id)
{
    return _ds.SearchLocations(id);
}

Everything works as it should, except that I am getting an HTTP 404 error when I try to get the resource like this:

/api/location/con

In this case, the method is not hit. The strange thing is that if I set any other string other than "con" as an id parameter, the controller method is being hit and works correctly!

This is happening while I am debugging my application on localhost with Cassini (same thing with IIS Express). There are no files named "con" in my project directory. After handling app error event, IntelliTrace revealed an HttpException with message: "Failed to map the path '/api/location/con'"...

Any clues? Is this a known bug?

Thanks in advance!


回答1:


Certain keywords are not allowed in the URL and CON is one of them. See this. The workaround is to include the following under <system.web> in your Web.config.

<httpRuntime relaxedUrlToFileSystemMapping="true" />


来源:https://stackoverflow.com/questions/17625764/asp-net-mvc-4-web-api-fails-to-map-path-containing-the-string-con

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