2013-12-18 29 views
0

上一个Asp.Net MVC 5个网站,我有以下文件的绝对地址:获取在MVC应用程序

@Url.Content("~/shareimage.jpg") 

这给了我一个相对URL。但我需要绝对的网址。

如何获取文件的绝对URL?

谢谢 米格尔

+0

可能重复的[asp.net mvc图像路径和虚拟目录](http://stackoverflow.com/questions/6509120/asp-net-mvc-image-path-and-virtual-directory) –

回答

2

这是这个问题的一个副本: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你在它扔。