WebAPI 请求抛出 404 错误...在同一请求上

WebAPI request throws 404 error ... on same request

我环顾四周,没有发现任何与我遇到的问题类似的问题,所以我想我会把它扔在这里,看看有什么问题。

我设置了控制器和方法。

public class BoothAPIController : ITApiControllerBase
{
    [HttpGet]
    public HttpResponseMessage GetActiveAssetNumbersLike([FromUri] String id)
    {
        HttpResponseMessage ret;

        // ... do some processing

        return ret;
    }
}

路由设置在Global.asax

protected void Application_Start(object sender, EventArgs e)
{

    GlobalConfiguration.Configuration.Routes.MapHttpRoute(
            name: "CustomApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional });


    GlobalConfiguration.Configuration.Routes.MapHttpRoute(
            name: "BoothWithDateAPI",
            routeTemplate: "api/{controller}/{boothID}/{year}/{month}/{day}");

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

}

并且这两个请求完美执行...

http://localhost:52211/api/BoothAPI/GetActiveAssetNumbersLike/PR
http://localhost:52211/api/BoothAPI/GetActiveAssetNumbersLike/PRN0

但是这个... returns 404 错误...

http://localhost:52211/api/BoothAPI/GetActiveAssetNumbersLike/PRN

失败请求的 Header 看起来像...

Cache-Control →private
Content-Length →2879
Content-Type →text/html; charset=utf-8
Date →Mon, 29 Aug 2016 12:53:08 GMT
Server →Microsoft-IIS/8.0
X-AspNet-Version →4.0.30319
X-Powered-By →ASP.NET
X-SourceFiles →= [string]

虽然成功的请求看起来像

Cache-Control →no-cache
Content-Length →7731
Content-Type →application/json; charset=utf-8
Date →Mon, 29 Aug 2016 13:13:43 GMT
Expires →-1
Pragma →no-cache
Server →Microsoft-IIS/8.0
X-AspNet-Version →4.0.30319
X-Powered-By →ASP.NET
X-SourceFiles → [string]

(耸耸肩)我不知道......我完全不知道为什么参数的一个变化会产生影响。

我对此进行了一些挖掘,自己也感到很惊讶。原来你试图发送的参数 PRNMS-DOS Device Driver

中的保留字

Below is a list of default device driver names.

PRN System list device, usually a parallel port

这个问题有问题的答案: IIS gives 404 when MS-DOS reserved names are used in parameters values

但是您应该意识到将 RelaxedUrlToFileSystemMapping 设置为 true 的潜在缺陷。请参阅 Scott Hanselman 的这篇文章:Experiments in Wackiness: Allowing percents, angle-brackets, and other naughty things in the ASP.NET/IIS Request URL