【问题标题】:MVC Url Request - Illegal Characters in pathMVC Url 请求 - 路径中的非法字符
【发布时间】:2015-08-07 15:50:18
【问题描述】:

概述:

  • 在 web.config 中配置了 404 错误页面并且可以正常工作
  • 使用 Umbraco v6.2
  • customErrors 设置为RemoteOnly

问题是当我在 URL 中输入“%7C”时,我收到了这个:

Illegal characters in path.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.ArgumentException: Illegal characters in path.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:    

[ArgumentException: Illegal characters in path.]
   System.IO.Path.GetExtension(String path) +14365864
   Umbraco.Core.UriExtensions.IsClientSideRequest(Uri url) +23
   Umbraco.Web.UmbracoModule.BeginRequest(HttpContextBase httpContext) +365
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +92
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +165

%7C 是一个管道“|”。 %7C 是唯一一个(我试过的)给我 YSOD 的。

如果我放了任何东西(我已经尝试了 30 多种组合),例如 %7A、%2A,那么有时它会给我这个错误而没有进入 404 页面,但绝不会出现 YSOD:

Bad Request - Invalid URL

HTTP Error 400. The request URL is invalid.

web.config

<customErrors mode="RemoteOnly" defaultRedirect="~/error-page" />

<httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL" existingResponse="Replace">
  <remove statusCode="404" subStatusCode="-1" />
  <remove statusCode="500" subStatusCode="-1" />
  <remove statusCode="503" subStatusCode="-1" />
  <error statusCode="404" prefixLanguageFilePath="" path="~/error-page" responseMode="ExecuteURL" />
  <error statusCode="500" prefixLanguageFilePath="" path="~/error-page" responseMode="ExecuteURL" />
  <error statusCode="503" prefixLanguageFilePath="" path="~/error-page" responseMode="ExecuteURL" />
</httpErrors>

在 Umbraco 中,有一个配置文件 (umbracoSettings.config),其中包含一个 XML 部分,其中包含字符替换。

例子:

 <char org="~"></char>
  <char org=" ">-</char>
  <char org="&quot;"></char>
  <char org="'"></char>
  <char org="%"></char>
  <char org="."></char>
  <char org="|"></char>

你可以看到管道在那里。如果我输入网址,

local.website.com/asdf|(注意管道),它被编码为local.website.com/asdf%7C

我尝试添加&lt;char org="%7C"&gt;&lt;/char&gt;,但它不起作用。我错过了什么吗?这是一个已知的错误吗?

【问题讨论】:

    标签: c# error-handling web-config umbraco


    【解决方案1】:

    这实际上不是错误或与 Umbraco 相关的问题,它是 ASP.NET 验证 HTTP 请求中的 URL 是否必须是有效的 Windows 文件路径的方式。

    你可以通过使用来解决它

    <httpRuntime relaxedUrlToFileSystemMapping="true" />
    

    根据documentation

    RelaxedUrlToFileSystemMapping 属性确定 URL 在 传入的 HTTP 请求将被验证。如果此属性为假, URL 是通过使用相同的规则来验证的,这些规则确定一个 Windows 文件系统路径有效。

    【讨论】:

    • 目前在我的 web.config 中。下&lt;system.web&gt; &lt;httpRuntime requestValidationMode="2.0" enableVersionHeader="false" relaxedUrlToFileSystemMapping="true" /&gt;
    【解决方案2】:

    必须添加到 web.config

      <remove statusCode="400" subStatusCode="-1" />
      <error statusCode="400" prefixLanguageFilePath="" path="~/error-page" responseMode="ExecuteURL" />
    

    当我打开错误customErrors="On" 后,这个错误就消失了。

    如果有人对此有更好的选择或推理,我愿意提供建议

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-05
      • 2019-08-30
      • 1970-01-01
      • 2019-05-30
      • 2011-12-19
      • 2012-03-05
      相关资源
      最近更新 更多