Welcome 微信登录

首页 / 网页编程 / ASP.NET / asp.net中的ResolveUrl 完美解决方案

asp.net中的ResolveUrl 完美解决方案2011-03-26 博客园 朱祁林根据我个人使用ASP.NET的经验,和通过在网上搜索。我发现,Page的ResolveUrl方法 给我们带来了一些比较严重的问题。

最常见的问题是在页面或控件以外的范围不 能使用它。

其他的问题都是bug。如它不能正确处理一些你给的URL。例如,尝试 Page.ResolveUrl("~/test.aspx?param=http://www.test.com")。结果与你输入字符串相 同,没做任何变动。使用Reflector查看asp.net代码,我发现将相对URL转换为绝对URL机 制都是先搜索string中的 "://",如果找到,就直接返回了。因此,如果你传递一个 带://的参数,查询字符串就是OK的。我们都知道的查询字符串参数应进行urlencode,但 如果没有,它应该仍然是可以接受URL。严重的是,要检查您的浏览器。

网上,建 议的方法是使用VirtualPathUtility.ToAbsolute,如果你在传递URL作为查询字符串,这 是相当不错和方便的,...否则,就会引发异常。如果是一个绝对URL,它也会抛出异常!

所以,我决定寻求一个最终解决方案。

首先,我要找搜索一个很好的变量 ,它能给我们在应用程序运行时的虚拟路径,不使用页面上下文。

我使用了 HttpRuntime.AppDomainAppVirtualPath。它能在任何地方使用-甚至在timer callback中 !它没有尾随斜线路径,ASP.NET作了特素处理,消除斜杠,不过,我们能修复它:-)

然后,我使用原ResolveUrl代码做了一些测试,找到怎么用AppVirtualPath取代 :

1、当URL以斜线开始(/或),也不会改动它!

2、当URL以〜/ 开始,它会被AppVirtualPath取代。

3、当URL是一个绝对URL,也不会改变它。

4、在任何其他情况下(甚至以〜开始,而不是斜杠),将追加URL到 AppVirtualPath。

5、每当它修改URL,还修复斜杠。删除双斜线,用/替换。

代码:

public static string ResolveUrl(string relativeUrl)
{
if (relativeUrl == null) throw new ArgumentNullException("relativeUrl");
if (relativeUrl.Length == 0 || relativeUrl[0] == "/" ||
relativeUrl[0] == "\") return relativeUrl;
int idxOfScheme =
relativeUrl.IndexOf(@"://", StringComparison.Ordinal);
if (idxOfScheme != -1)
{
int idxOfQM = relativeUrl.IndexOf("?");
if (idxOfQM == -1 || idxOfQM > idxOfScheme) return relativeUrl;
}
StringBuilder sbUrl = new StringBuilder();
sbUrl.Append (HttpRuntime.AppDomainAppVirtualPath);
if (sbUrl.Length == 0 || sbUrl[sbUrl.Length - 1] != "/") sbUrl.Append("/");
// found question mark already? query string, do not touch!
bool foundQM = false;
bool foundSlash; // the latest char was a slash?
if (relativeUrl.Length > 1
&& relativeUrl[0] == "~"
&& (relativeUrl[1] == "/" || relativeUrl[1] == "\"))
{
relativeUrl = relativeUrl.Substring(2);
foundSlash = true;
}
else foundSlash = false;
foreach (char c in relativeUrl)
{
if (!foundQM)
{
if (c == "?") foundQM = true;
else
{
if (c == "/" || c == "\")
{
if (foundSlash) continue;
else
{
sbUrl.Append("/");
foundSlash = true;
continue;
}
}
else if (foundSlash) foundSlash = false;
}
}
sbUrl.Append(c);
}
return sbUrl.ToString();
}

在完成代码后和比较原来ResolveUrl测试一遍又一遍,我开始测试性 能...在大多数情况下,我的代码执行速度比原来快ResolveUrl 2.7倍!我也在循环内部 进行测试,用不同的URL执行代码100000次。

出处: http://zhuqil.cnblogs.com