从相对路径(重构方法)获取绝对URL [英] Get Absolute URL from Relative path (refactored method)
问题描述
我真的很惊讶,没有提供原生的.NET方法从一个相对URL得到一个绝对的URL。我知道这已经讨论过很多次,但从来没有遇到过处理这口井令人满意的方法。你能帮助微调下面的方法?
我想我需要的左侧是自动选择的协议,而不是硬编码它(HTTP / HTTPS)。别的我失踪(警告,性能等)?
公共静态字符串GetAbsoluteUrl(字符串URL)
{
//验证输入对于已经绝对URL
如果(url.StartsWith(HTTP://,StringComparison.OrdinalIgnoreCase)
|| url.StartsWith(https://开头,StringComparison.OrdinalIgnoreCase))
{
返回URL;
}
//获取页面引用上下文处理
页= HttpContext.Current.Handler的页面;
//解决路径应用程序处理前
如果(url.StartsWith(〜/))
{
URL = page.ResolveUrl(URL);
}
//建立并返回绝对URL
返回HTTP://+ page.Request.ServerVariables [SERVER_NAME] +/
+ url.TrimStart('/');
}
这是我一直以来的方法来这个小滋扰。注意,这里使用的<一个href="http://msdn.microsoft.com/en-us/library/system.web.virtualpathutility.toabsolute.aspx">VirtualPathUtility.ToAbsolute(relativeUrl)允许该方法被声明为静态类的扩展。
///&LT;总结&gt;
///所提供的应用程序相对路径转换为包含绝对URL
///完整主机名
///&LT; /总结&gt;
///&LT; PARAM NAME =relativeUrl&GT;应用程序相对路径和LT; /参数&GT;
///&LT;返回&GT;提供relativeUrl参数作为完整的URL和LT; /回报&GT;
///&LT;示例&gt;〜/路径/要/富到http://www.web.com/path/to/foo</example>
公共静态字符串ToAbsoluteUrl(此字符串relativeUrl){
如果(string.IsNullOrEmpty(relativeUrl))
返回relativeUrl;
如果(HttpContext.Current == NULL)
返回relativeUrl;
如果(relativeUrl.StartsWith(/))
relativeUrl = relativeUrl.Insert(0,〜);
如果(!relativeUrl.StartsWith(〜/))
relativeUrl = relativeUrl.Insert(0,〜/);
VAR URL = HttpContext.Current.Request.Url;
VAR端口= url.Port!= 80? (:+ url.Port):的String.Empty;
返回的String.Format({0}:// {1} {2} {3},
url.Scheme,url.Host,港口,VirtualPathUtility.ToAbsolute(relativeUrl));
}
I am really surprised that there is no native .NET method to get an absolute url from a relative url. I know this has been discussed many times, but never have come across a satisfactory method that handles this well. Can you help fine tune the method below?
I think all I need left is to auto choose the protocol instead of hard coding it (http/https). Anything else I am missing (caveats, performance, etc)?
public static string GetAbsoluteUrl(string url)
{
//VALIDATE INPUT FOR ALREADY ABSOLUTE URL
if (url.StartsWith("http://", StringComparison.OrdinalIgnoreCase)
|| url.StartsWith("https://", StringComparison.OrdinalIgnoreCase))
{
return url;
}
//GET PAGE REFERENCE FOR CONTEXT PROCESSING
Page page = HttpContext.Current.Handler as Page;
//RESOLVE PATH FOR APPLICATION BEFORE PROCESSING
if (url.StartsWith("~/"))
{
url = page.ResolveUrl(url);
}
//BUILD AND RETURN ABSOLUTE URL
return "http://" + page.Request.ServerVariables["SERVER_NAME"] + "/"
+ url.TrimStart('/');
}
This has always been my approach to this little nuisance. Note the use of VirtualPathUtility.ToAbsolute(relativeUrl) allows the method to be declared as an extension in a static class.
/// <summary>
/// Converts the provided app-relative path into an absolute Url containing the
/// full host name
/// </summary>
/// <param name="relativeUrl">App-Relative path</param>
/// <returns>Provided relativeUrl parameter as fully qualified Url</returns>
/// <example>~/path/to/foo to http://www.web.com/path/to/foo</example>
public static string ToAbsoluteUrl(this string relativeUrl) {
if (string.IsNullOrEmpty(relativeUrl))
return relativeUrl;
if (HttpContext.Current == null)
return relativeUrl;
if (relativeUrl.StartsWith("/"))
relativeUrl = relativeUrl.Insert(0, "~");
if (!relativeUrl.StartsWith("~/"))
relativeUrl = relativeUrl.Insert(0, "~/");
var url = HttpContext.Current.Request.Url;
var port = url.Port != 80 ? (":" + url.Port) : String.Empty;
return String.Format("{0}://{1}{2}{3}",
url.Scheme, url.Host, port, VirtualPathUtility.ToAbsolute(relativeUrl));
}
这篇关于从相对路径(重构方法)获取绝对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!