关于c#:asp.net mvc映像路径和虚拟目录

asp.net mvc image path and virtual directory

我知道这一定是重复的,但是我一直在浏览关于这方面的大量信息,但我无法使其正常工作。

我正在尝试让站点在客户端的服务器上工作,并且他们将该站点安装在虚拟目录中。我在本地没有此设置,所以我在这里盲目飞行。

我正在尝试建立图像的路径。 (适用于Facebook OpenGraph元数据)。

我需要图像的路径是完全限定的绝对URL。我尝试了很多事情,但似乎没有任何效果。下面的代码输出一个相对URL,但这将不起作用。

1
2
<% var urlHelper = VirtualPathUtility.ToAbsolute("~/static/images/image.jpg");%>
<meta property="og:image" content="<%=urlHelper%>" />

输出:

1
<meta property="og:image" content="/static/images/image.jpg" />

我也尝试过:

1
2
3
<% var serverHost = HttpContext.Current.Request.Url; %>
<meta property="og:image"
          content="<%=serverHost +"/static/images/image.jpg"%>" />

但这产生了:

1
2
<meta property="og:image"
   content="http://localhost:51863/ViewFile.aspx/static/images/image.jpg" />

我正在寻找http://example.com/virtualdirectory/static/images/image.jpg

任何帮助都感激不尽。我真的不想硬编码网址。

谢谢,
史考特

编辑

我忽略了提及,我的第一次尝试是Url.Content("?/ .... jpg),但它输出的是相对网址,而不是绝对网址。


这段代码

1
2
3
4
5
6
7
8
9
10
11
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(例如foo/bar/image.png)可能无法解决您认为的方式,特别是如果URI引用目录的话,那么您将获得默认页面(即,http://localhost/foo/bar可能是一个实际的资源(在这种情况下URI是完整的,或者可能是不完整的),在这种情况下,Asp.Net MVC的路由会填空。所有请求都是原始URI。如果要解决此问题,您将需要获取请求的RouteData并询问其详细信息。

这是如何使用植根于http://localhost/MyApp的Web应用程序以及从Home控制器的About视图以不同方式调用Html帮助程序方法来解决问题的。

  • ~

    • 解析为http://localhost/MyApp/
  • /

    • 解析为`http:// localhost /
  • ~/

    • 解析为http://localhost/MyApp/
  • foo/bar/myImage.png

    • 解析为http://localhost/MyApp/Home/foo/bar/myImage.png
  • /foo/bar/myImage.png

    • 解析为http://localhost/foo/bar/myImage.png
  • ~/foo/bar/myImage.png

    • 解析为http://localhost/MyApp/foo/bar/myImage.png
  • http://somesite.com/foo/bar/myImage.png

    • 解析为http://somesite.come/foo/bar/myImage.png
  • http://somesite.com:123/foo/bar/myImage.png

    • 解析为http://somesite.come:123/foo/bar/myImage.png
  • ftp://somesite.com/foo/bar/myImage.png

    • 解析为ftp://somesite.come:123/foo/bar/myImage.png
  • mailto://[email protected]


您可以编写一个小的扩展方法:

1
2
3
4
5
6
7
8
9
10
11
12
public static class UrlExtensions
{
    public static string AbsoluteContent(this UrlHelper url, string contentPath)
    {
        var requestUrl = url.RequestContext.HttpContext.Request.Url;
        return string.Format(
           "{0}{1}",
            requestUrl.GetLeftPart(UriPartial.Authority),
            url.Content(contentPath)
        );
    }
}

接着:

1
<meta property="og:image" content="<%= Url.AbsoluteContent("~/static/images/image.jpg") %>" />

将输出例如:

1
<meta property="og:image" content="http://localhost:7864/static/images/image.jpg" />

UrlHelper上使用Content方法。

http://msdn.microsoft.com/zh-CN/library/system.web.mvc.urlhelper.content.aspx


您应该使用路由来解析您的URL。

我个人希望遵循此处的最佳做法指南:

Create Extension methods of UrlHelper
to generate your url from Route

您可能需要这些静态图像的扩展方法:

1
2
3
4
public static string StaticImage(this UrlHelper helper, string fileName)
    {
        return helper.Content("~/static/images/{0}".FormatWith(fileName));
    }

那么您可以执行以下操作:

1
<meta property="og:image" content="<%: Url.StaticImage("image.jpg") %>" />