我怎样才能得到可靠的实际的URL,即使有百分之恩路径codeD部分? [英] How can I reliably get the actual URL, even when there are percent-encoded parts in the path?
问题描述
IIS和ASP.NET(MVC)<一个href=\"http://stackoverflow.com/questions/7913830/is-iis-performing-an-illegal-character-substitution-if-so-how-to-stop-it\">has在路径的使用URL时%编码一些小问题(而不是查询字符串,查询字符串是罚款)。我怎样才能解决这个问题?即我怎样才能得到被请求的实际URL?
IIS and ASP.NET (MVC) has some glitches when working with urls with %-encoding in the path (not the query-string; the query-string is fine). How can I get around this? i.e. how can I get the actual URL that was requested?
例如,如果我浏览到 / X%3FA%3DB
和(单独)为 / X?A = B
- 他们都报告 .Request.Url
为 / X A = b
- ?因为恩codeD数据的路径的不正确的报道。
For example, if I navigate to /x%3Fa%3Db
and (separately) to /x?a=b
- both of them report the .Request.Url
as /x?a=b
- because the encoded data in the path is reported incorrectly.
推荐答案
我已经上涨,这是看底层服务器变量的方式;在网址
变量包含的德codeD 的价值;在 QUERY_STRING
变量包含仍然恩codeD查询。我们不能只是调用的连接code 的关于网址
部分,因为还包含一部开拓创新的 /
等在他们的原始形式 - 如果我们一味地连接code,我们会得到不想要的%2F
值,整个事情;然而,可以拉开它,并发现有问题的情况:
The way I've tacked this is to look at the underlying server-variables; the URL
variable contains a decoded value; the QUERY_STRING
variable contains the still-encoded query. We can't just call encode on the URL
part, because that also contains the orignal /
etc in their original form - if we blindly encode the entire thing we'll get unwanted %2f
values; however, can pull it apart and spot problematic cases:
private static readonly Regex simpleUrlPath = new Regex("^[-a-zA-Z0-9_/]*$", RegexOptions.Compiled);
private static readonly char[] segmentsSplitChars = { '/' };
// ^^^ avoids lots of gen-0 arrays being created when calling .Split
public static Uri GetRealUrl(this HttpRequest request)
{
if (request == null) throw new ArgumentNullException("request");
var baseUri = request.Url; // use this primarily to avoid needing to process the protocol / authority
try
{
var vars = request.ServerVariables;
var url = vars["URL"];
if (string.IsNullOrEmpty(url) || simpleUrlPath.IsMatch(url)) return baseUri; // nothing to do - looks simple enough even for IIS
var query = vars["QUERY_STRING"];
// here's the thing: url contains *decoded* values; query contains *encoded* values
// loop over the segments, encoding each separately
var sb = new StringBuilder(url.Length * 2); // allow double to be pessimistic; we already expect trouble
var segments = url.Split(segmentsSplitChars);
foreach (var segment in segments)
{
if (segment.Length == 0)
{
if(sb.Length != 0) sb.Append('/');
}
else if (simpleUrlPath.IsMatch(segment))
{
sb.Append('/').Append(segment);
}
else
{
sb.Append('/').Append(HttpUtility.UrlEncode(segment));
}
}
if (!string.IsNullOrEmpty(query)) sb.Append('?').Append(query); // query is fine; nothing needing
return new Uri(baseUri, sb.ToString());
}
catch (Exception ex)
{ // if something unexpected happens, default to the broken ASP.NET handling
GlobalApplication.LogException(ex);
return baseUri;
}
}
这篇关于我怎样才能得到可靠的实际的URL,即使有百分之恩路径codeD部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!