WebApi 代理为具有无效字符的 URL 抛出 404

WebApi Proxy throwing 404 for URLs with invalid chars

我正在使用 C# .NET WebApi 创建一个 API 代理,这里是我拥有的适用于大多数调用的基本代码:

[HttpGet]
[Route("api/proxy/{*.}")]
public async Task<HttpResponseMessage> Get()
{
   // some route here...
}

但是当我有一个带有特殊字符的 URL 时,例如 : (%3F) 或 / (%2F) 它会失败并显示 404。如果我删除特殊字符,它会起作用。

http://localhost:3000/api/proxy/viewing/v1/items/urn%3Aadsk.viewing%3Afs.file%3AdXJuOmVUE_dmVyc2lvbj0x%2Foutput%2Fd4b6ef1c-8d219bd84c9d%2F0.pf?domain=http%3A%2F%2Flocalhost%3A3000

并在 web.config 上尝试了一些建议,但一无所获:

<system.web>
  <httpRuntime targetFramework="4.6" requestPathInvalidCharacters=""/>
  <pages validateRequest="false" />
</system.web>

所有其他 WebApi 端点都使用这样的 Global.asax:

public class Global : System.Web.HttpApplication
{
  protected void Application_Start(object sender, EventArgs e)
  {
    GlobalConfiguration.Configure(Config.WebApiConfig.Register);
  }
}

和 Config.WebApiConfig:

public class WebApiConfig
{
  public static void Register(HttpConfiguration config)
  {
    config.MapHttpAttributeRoutes();
  }
}

根据答案 MVC WEB API routing fails when url contains encoded ampersand,您需要从 httpruntimerequestPathInvalidCharacters 属性 中删除您认为有效的字符:

<httpruntime requestvalidationmode="2.0">
             requestPathInvalidCharacters="*,:,&amp;,\"
             relaxedUrlToFileSystemMapping="true" />

但是将这些参数作为查询字符串传递会更安全。

编辑:

我已经从你的问题 http://localhost:3000/proxy/target/v1/urn%3Aparam%2Foutpu?domain=http%3A%2F%2Flocalhost%3A3000 中测试了 url 并且它通过简单地允许 % 字符来工作。因此,您需要在 requestPathInvalidCharacters 属性、i.g: %,&amp;,*,\ 中列出允许的字符。我当前的配置是:

<httpRuntime targetFramework="4.6.1" requestPathInvalidCharacters="%" />

编辑 2:

我进行了更多研究并找到了 ASP.net MVC4 WebApi route with file-name in it 的答案。简单的解决方案是将 runAllManagedModulesForAllRequests 属性 添加到 Web 配置中的模块部分。如果您检查已接受的答案,您可以尝试微调解决方案以减少 运行 所有托管模块的影响。

总而言之,您需要修改以下部分。我只从 requestPathInvalidCharacters 的默认值中删除了 :,它适用于已经解码的 URL,与编码 URL 中的 % 个字符无关:

<system.web>
  <httpRuntime requestPathInvalidCharacters="&lt;,&gt;,*,%,&amp;,\,?"/>
</system.web>

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true">
  ...
  </modules>
</system.webServer>

尝试 web.config 中的这 2 处更改。

<system.web>
   <httpRuntime targetFramework="4.6" requestPathInvalidCharacters=""/>
</system.web>
...
<system.webServer>
<handlers>
  ...
  <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*" verb="*" type="System.Web.Handlers.TransferRequestHandler"
    preCondition="integratedMode,runtimeVersionv4.0"/>
</handlers>

注意:我在 requestPathInvalidCharacters 中设置了一个空列表,并将处理程序中的路径过滤器更改为“*”。