【问题标题】:WebAPI request throws 404 error ... on same requestWebAPI 请求引发 404 错误...在同一请求上
【发布时间】:2016-08-29 13:14:43
【问题描述】:

我环顾四周,并没有偶然发现与我遇到的问题相似的任何东西,所以我想我会把它扔在这里看看,如果有什么问题的话。

我设置了控制器和方法。

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]

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

【问题讨论】:

  • 我发现 Content-Type 有所不同。当它失败时,它正在发送一个文本/html。您如何发送这些请求?通过应用程序,还是通过 Postman?
  • 另外,为什么要在 Global.asax 中设置路线?您的 App_Start 文件夹中没有 WebApiConfig 文件吗?
  • 请求是相同的,都是从Postman发出的,只是参数字符串不同。至于 Global.asax ... 为方便起见。我没有太多的配置,所以没有必要打破它。随着应用程序的增长,我们可能会对其进行一些重构。

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


【解决方案1】:

我对此做了一点挖掘,我自己也很惊讶。原来你要发送的参数PRNMS-DOS Device Driver中的保留字

以下是默认设备驱动程序名称列表。

PRN 系统列表设备,通常是一个并口

这个问题有一个问题的答案: 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

【讨论】:

  • 谢谢先生!这解决了问题,虽然我无法想象为什么这些保留名称与 Web 服务有任何关系......
  • 它们与文件系统有关。我提供的链接详细解释了为什么。每天要学习一件事!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-18
  • 2013-01-29
  • 1970-01-01
  • 2021-01-14
  • 1970-01-01
  • 2015-07-31
  • 2015-08-30
相关资源
最近更新 更多