我有一個相對路徑爲「〜/ pages/mypage.aspx」的頁面。如何獲得相對uri的絕對uri?
我試過使用VirtualPathUtility.ToAbsolute(@「〜/ pages/mypage.aspx」)希望得到絕對的uri,但它只返回相同的相對路徑。
我怎麼能從這個相對的uri中獲得絕對的uri?
感謝,
我有一個相對路徑爲「〜/ pages/mypage.aspx」的頁面。如何獲得相對uri的絕對uri?
我試過使用VirtualPathUtility.ToAbsolute(@「〜/ pages/mypage.aspx」)希望得到絕對的uri,但它只返回相同的相對路徑。
我怎麼能從這個相對的uri中獲得絕對的uri?
感謝,
摘自我用這個方法:
private static string GetAbsoluteUrl(string relativeUrl)
{
var applicationUrl = String.Empty;
// remove ~
if (relativeUrl.StartsWith("~"))
{
applicationUrl = relativeUrl.Substring(1);
}
applicationUrl = (HttpContext.Current.Request.ApplicationPath + applicationUrl)
.Replace("//", "/");
var baseUrl = String.Format("{0}://{1}:{2}",
HttpContext.Current.Request.Url.Scheme,
HttpContext.Current.Request.Url.Host,
HttpContext.Current.Request.Url.Port);
return baseUrl + applicationUrl;
}
public static string ResolveUrl(string originalUrl)
{
if (originalUrl == null)
return null;
// *** Absolute path - just return
if (originalUrl.IndexOf("://") != -1)
return originalUrl;
// *** Fix up image path for ~ root app dir directory
if (originalUrl.StartsWith("~"))
{
string newUrl = "";
if (HttpContext.Current != null)
newUrl = HttpContext.Current.Request.ApplicationPath +
originalUrl.Substring(1).Replace("//", "/");
else
// *** Not context: assume current directory is the base directory
throw new ArgumentException("Invalid URL: Relative URL not allowed.");
// *** Just to be sure fix up any double slashes
return newUrl;
}
return originalUrl;
}
HttpContext.Current.Request.ApplicationPath僅返回 「/」 所以這種方法行不通無論是。任何想法? – Dynamic 2011-04-07 08:53:37