IIS 10.0、VS 2022、.NET Framework 4.7.2 Web API
我继承了一个 web api 应用程序,有人告诉我它上次发布时 100% 工作(至少一年前,也许 2)。但是,现在当属性包含句点时(在使用 IIS 的 IDE 中以及在服务器上发布到 IIS 时),我得到 404 not found。
我尝试用两种方法创建一个新的 web api 应用程序:
[HttpPost]
[Route("BatchData/ProcessResponseByString/{requestId}")]
public HttpResponseMessage ProcessResponseByString(string requestId)
{
return new HttpResponseMessage()
{
Content = new StringContent($"String:{requestId}")
};
}
[HttpPost]
[Route("BatchData/ProcessResponseByDouble/{requestId:double}")]
public HttpResponseMessage ProcessResponse(double requestId)
{
return new HttpResponseMessage()
{
Content = new StringContent($"Double:{requestId}")
};
}
这些网址有效: https://localhost:44352/BatchData/ProcessResponseByString/12345 https://localhost:44352/BatchData/ProcessResponseByDouble/12345
这些 url 失败并显示 404 not found: https://localhost:44352/BatchData/ProcessResponseByString/12345.67 https://localhost:44352/BatchData/ProcessResponseByDouble/12345.67
也许 IIS 升级改变了 IIS 中路由的解析方式?
关于如何解决这个问题的任何想法?
谢谢。
@Paritosh 发布了链接(为什么我的带有双参数的 Web API 方法没有被调用?)并建议了 / 解决方法(感谢链接),但对我有用的答案是在 / 建议之后 - 更新 webconfig
对于 ASP.NET/IIS,Suprotim Agarwal 在此处提供 web.config 重写代码:https://www.devcurry.com/2015/07/allowing-dots-in-aspnet-mvc-paths.html - 此重写当它不是对目录或文件的请求时,以交互方式在 URL 的末尾添加一个斜杠(包含句点):
<system.webServer>
<rewrite>
<rules>
<clear />
<rule name="AddTrailingSlashRule1" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}/" />
</rule>
</rules>
</rewrite>
</system.webServer>
编辑 2023-03-17:不幸的是我不得不放弃这种方法,因为当我使用它时,我的 Angular 应用程序会产生以下错误:
Failed to load module script: Expected a Javascript module script but the server responded with a MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.
很难确定这个重写是导致错误的原因,因为当我从 web.config 中删除它时,错误仍然存在,即使我回收了我的 IIS 应用程序池。每个浏览器都存在一些问题——如果我启动一个不同的浏览器,web.config 的状态(不再重写)将意味着该应用程序再次运行。