gpt4 book ai didi

asp.net - 允许冒号(IIS/Azure 中的 :) in URL for ASP. NET Core

转载 作者:行者123 更新时间:2023-12-05 02:19:27 27 4
gpt4 key购买 nike

我有一个 ASP.NET Core 应用程序,正在部署到 Azure,该应用程序在 URL 中接收包含冒号(时间戳)的字符串。

例如:http://localhost:5000/Servers/208.100.45.135/28000/2017-03-15T07:03:43+00:00,或http:///localhost:5000/Servers/208.100.45.135/28000/2017-03-15T07%3a03%3a43%2B00%3a00 URL 编码。

使用 Kestrel (dotnet run) 在本地运行时效果非常好,但在部署到 Azure 后,我收到此错误:您正在查找的资源已被删除,其名称已被删除已更改,或暂时不可用。

快速搜索后发现,这是由于 URL 中使用了无效字符(即冒号)造成的。传统的修复方法是将此部分添加到 web.config 中:

 <system.web>
<httpRuntime requestPathInvalidCharacters="" />
</system.web>

但是,将其添加到 Azure 上的 web.config 后,我没有观察到任何变化。我想这是由于 ASP.NET Core 托管模型的差异造成的。

这是我当前的web.config:

<configuration>
<system.web>
<httpRuntime requestPathInvalidCharacters=""/>
<pages validateRequest="false" />
</system.web>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\Server.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" />
</system.webServer>
</configuration>

以及相关的 Controller header ...

[HttpGet]
[Route("{serverIpAddress}/{serverPort}/{approxMatchStartTimeStr}")]
public IActionResult GetMatchEvents(string serverIpAddress, string serverPort, DateTimeOffset approxMatchStartTimeStr)
{
...
}

如何让 IIS/Azure 允许 URL 中使用冒号字符?

最佳答案

您遇到的问题与路径中的冒号 (:) 无关,它实际上是 plus (+) that IIS doesn't like 。加号是否编码为“+”或“%2B”并不重要。您有两个选择:

  1. 将 plus/DateTimeOffset 从路径移至 IIS 不介意的查询字符串。
  2. 将 IIS 请求过滤模块配置为“allowDoubleEscaping”。

示例 web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<security>
<requestFiltering allowDoubleEscaping="true" />
</security>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="dotnet" arguments=".\Server.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false" />
</system.webServer>
</configuration>

当前 web.config 的 system.web 部分与 ASP.NET Core 无关。

关于asp.net - 允许冒号(IIS/Azure 中的 :) in URL for ASP. NET Core,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42819825/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com