ASP.NET 4: 允许对URL中的特殊字符串放宽要求

在做 URL Rewrite 时可能遇到这类问题,即想要使用的 Rewrite 后的目标 URL 中会含有特殊字符,或者说不能直接映射到操作系统的文件系统允许的文件名格式,比如以 “.” 结尾等等。如果不做特殊处理的话则 ASP.NET 引擎会报 "CheckSuspiciousPhysicalPath” 的错误。

解决办法:

1. ASP.NET 版本小于4.0的情况,可以在 Global.asax.cs 中处理 PreSendRequestHeaders 事件,例如:

protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
{
HttpResponse response = this.Context.Response;
HttpRequest request = this.Context.Request;
if (request.RawUrl.EndsWith("."))
{
response.ClearContent();
response.StatusCode = 200;
response.StatusDescription = "OK";
response.SuppressContent = false;
response.ContentType = "text/plain";
response.Write("You have dot at the end of the url, this is allowed, but not by ASP.NET, but I caught you!");
response.End();
}
}

2. ASP.NET 版本 >= 4.0,在 web.config 中添加一个设置即可:

<configuration>
<system.web>
<httpRuntime relaxedUrlToFileSystemMapping="true"/>
<!-- ... your other settings ... -->
</system.web>
</configuration>

参考:

http://haacked.com/archive/2010/04/29/allowing-reserved-filenames-in-URLs.aspx

http://stackoverflow.com/questions/1126735/problem-with-a-url-that-ends-with-20

转载于:https://www.cnblogs.com/RChen/archive/2011/04/12/2013770.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值