asp.net-web-api - WebAPI 请求在同一请求上抛出 404 错误...

标签 asp.net-web-api asp.net-web-api-routing

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

我已经设置了 Controller 和方法。

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

但是这个...返回 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

关于asp.net-web-api - WebAPI 请求在同一请求上抛出 404 错误...,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39207252/

相关文章:

c# - OData 无法识别我的集合属性

c# - 尽管在响应对象上配置了缓存控制 header ,但未在响应中发送

c# - 我的 web api 2 Controller 需要路由

ASP.NET Core Web API 在本地运行,但不在 Azure 应用服务上运行

使用 PUT 的 ASP.NET Web API 路由

c# - Linq to SQL 通用存储库模式更新问题

c# - Task.ContinueWith执行顺序

asp.net-web-api - HttpRouteBuilder - 它去了哪里,为什么?

C# WebApi Controller(States Controller目前有2个Get方法,1个返回List,第2个返回Single Item)

c# - 从代码和网站调用Web API