MVC Url请求 - 路径中存在非法字符

3

概述:

  • 已在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" />

这段代码是关于网站错误处理的配置,其中mode属性值为"RemoteOnly"表示只有在远程访问时才会显示错误信息,defaultRedirect属性则指定了默认的重定向页面,即在发生错误时跳转到指定的页面。请注意保留HTML标签。
<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>

您可以看到管道符号在这里。如果我将其放入URL中,local.website.com/asdf|(注意管道符号),它会被编码为local.website.com/asdf%7C
我尝试添加<char org="%7C"></char>,但它不起作用。我有什么遗漏的吗?这是一个已知的错误吗?
2个回答

3

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

您可以通过使用

<httpRuntime relaxedUrlToFileSystemMapping="true" />

根据 文档
RelaxedUrlToFileSystemMapping 属性确定如何验证传入 HTTP 请求中的 URL。如果该属性为 false,则使用确定 Windows 文件系统路径是否有效的相同规则验证 URL。

这是我当前的web.config文件中的内容。在<system.web>下面,有<httpRuntime requestValidationMode="2.0" enableVersionHeader="false" relaxedUrlToFileSystemMapping="true" /> - Rob Scott

0

必须添加到web.config

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

而且一旦我打开了错误提示 customErrors="On",这个错误就消失了。

如果有人有更好的替代方案或者对此有理由解释,欢迎提出建议。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接