上一個Asp.Net MVC 5個網站,我有以下文件的絕對地址:獲取在MVC應用程序
@Url.Content("~/shareimage.jpg")
這給了我一個相對URL。但我需要絕對的網址。
如何獲取文件的絕對URL?
謝謝 米格爾
上一個Asp.Net MVC 5個網站,我有以下文件的絕對地址:獲取在MVC應用程序
@Url.Content("~/shareimage.jpg")
這給了我一個相對URL。但我需要絕對的網址。
如何獲取文件的絕對URL?
謝謝 米格爾
Url.Action或url.RouteUrl已指定的協議給你的絕對URL。
<%= Url.Action("About", "Home", null, "http") %><br />
<%= Url.RouteUrl("Default", new { Action = "About" }, "http") %><br />
http://captaincodeman.com/2010/02/03/absolute-urls-using-mvc-without-extension-methods/
這是這個問題的一個副本:asp.net mvc image path and virtual directory。
我用這個幫手自己:
public static class Helpers
{
public static Uri FullyQualifiedUri(this HtmlHelper html , string relativeOrAbsolutePath)
{
Uri baseUri = HttpContext.Current.Request.Url ;
string path = UrlHelper.GenerateContentUrl(relativeOrAbsolutePath, new HttpContextWrapper(HttpContext.Current)) ;
Uri instance = null ;
bool ok = Uri.TryCreate(baseUri , path , out instance) ;
return instance ; // instance will be null if the uri could not be created
}
}
這應該給你一個完全組隊參加絕對URI的幾乎任何URI你在它扔。
可能重複的[asp.net mvc圖像路徑和虛擬目錄](http://stackoverflow.com/questions/6509120/asp-net-mvc-image-path-and-virtual-directory) –