问题
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