0
A
回答
1
http://www.west-wind.com/weblog/posts/2007/Sep/18/ResolveUrl-without-Page(在評論)
/// <summary>
/// Returns a site relative HTTP path from a partial path starting out with a ~.
/// Same syntax that ASP.Net internally supports but this method can be used
/// outside of the Page framework.
///
/// Works like Control.ResolveUrl including support for ~ syntax
/// but returns an absolute URL.
/// </summary>
/// <param name="originalUrl">Any Url including those starting with ~</param>
/// <returns>relative url</returns>
public static string ResolveUrl(string originalUrl)
{
if (string.IsNullOrEmpty(originalUrl))
return originalUrl;
// *** Absolute path - just return
if (IsAbsolutePath(originalUrl))
return originalUrl;
// *** We don't start with the '~' -> we don't process the Url
if (!originalUrl.StartsWith("~"))
return originalUrl;
// *** Fix up path for ~ root app dir directory
// VirtualPathUtility blows up if there is a
// query string, so we have to account for this.
int queryStringStartIndex = originalUrl.IndexOf('?');
if (queryStringStartIndex != -1)
{
string queryString = originalUrl.Substring(queryStringStartIndex);
string baseUrl = originalUrl.Substring(0, queryStringStartIndex);
return string.Concat(
VirtualPathUtility.ToAbsolute(baseUrl),
queryString);
}
else
{
return VirtualPathUtility.ToAbsolute(originalUrl);
}
}
/// <summary>
/// This method returns a fully qualified absolute server Url which includes
/// the protocol, server, port in addition to the server relative Url.
///
/// Works like Control.ResolveUrl including support for ~ syntax
/// but returns an absolute URL.
/// </summary>
/// <param name="ServerUrl">Any Url, either App relative or fully qualified</param>
/// <param name="forceHttps">if true forces the url to use https</param>
/// <returns></returns>
public static string ResolveServerUrl(string serverUrl, bool forceHttps)
{
if (string.IsNullOrEmpty(serverUrl))
return serverUrl;
// *** Is it already an absolute Url?
if(IsAbsolutePath(serverUrl))
return serverUrl;
string newServerUrl = ResolveUrl(serverUrl);
Uri result = new Uri(HttpContext.Current.Request.Url, newServerUrl);
if (!forceHttps)
return result.ToString();
else
return ForceUriToHttps(result).ToString();
}
/// <summary>
/// This method returns a fully qualified absolute server Url which includes
/// the protocol, server, port in addition to the server relative Url.
///
/// It work like Page.ResolveUrl, but adds these to the beginning.
/// This method is useful for generating Urls for AJAX methods
/// </summary>
/// <param name="ServerUrl">Any Url, either App relative or fully qualified</param>
/// <returns></returns>
public static string ResolveServerUrl(string serverUrl)
{
return ResolveServerUrl(serverUrl, false);
}
/// <summary>
/// Forces the Uri to use https
/// </summary>
private static Uri ForceUriToHttps(Uri uri)
{
// ** Re-write Url using builder.
UriBuilder builder = new UriBuilder(uri);
builder.Scheme = Uri.UriSchemeHttps;
builder.Port = 443;
return builder.Uri;
}
private static bool IsAbsolutePath(string originalUrl)
{
// *** Absolute path - just return
int IndexOfSlashes = originalUrl.IndexOf("://");
int IndexOfQuestionMarks = originalUrl.IndexOf("?");
if (IndexOfSlashes > -1 &&
(IndexOfQuestionMarks < 0 ||
(IndexOfQuestionMarks > -1 && IndexOfQuestionMarks > IndexOfSlashes)
)
)
return true;
return false;
}
2
相關問題
- 1. 從靜態基類方法調用靜態派生類方法
- 2. 使用非靜態方法中的內部類的靜態類
- 3. 使用通用類的靜態方法
- 4. 在Dao類中使用靜態方法或非靜態方法?
- 5. 調用類類型的靜態方法
- 6. 使用靜態方法的FlashDevelop類
- 7. 調用靜態類方法
- 8. 使用類型調用靜態方法
- 9. 從ASP調用頁面靜態類的靜態方法
- 10. 從非靜態類動態調用非靜態方法的最簡單方法?
- 11. 靜態方法getDBConnection()從類型DBConnection的應以靜態方式
- 12. DLLEXPORT靜態類的方法
- 13. 類名的靜態方法?
- 14. Python類的靜態方法
- 15. 麻煩從子類的靜態方法
- 16. 在靜態類的靜態方法的參數中使用它?
- 17. 使用GroovyMock或Spock中的類似方法模擬靜態方法
- 18. 靜態類和靜態方法
- 19. 從抽象類中的靜態方法調用非靜態方法(C++)
- 20. 如何從同一類的靜態方法調用非靜態方法?
- 21. 不能使從類型JobsListAdapter的靜態引用非靜態方法用getFilter()
- 22. C#類實例的靜態方法VS靜態類的內存使用情況
- 23. (Java)靜態通用方法與泛型類靜態方法
- 24. 無法從靜態方法訪問靜態方法Android:getResources()從類型ContextWrapper
- 25. 如何從另一個類的靜態類中調用方法?
- 26. PHP OOP - 從類內的另一個類調用靜態方法
- 27. 從父類調用子類的靜態方法
- 28. 從靜態方法實例化子類
- 29. 從靜態方法訪問類成員
- 30. 非靜態類中的靜態方法和靜態類中的靜態方法有什麼區別?
VirtualPathUtility.ToAbsolute它是利用ToAbsolute或ToAppRelative方法最佳方案。謝謝 :) – Tuizi