【问题标题】:asp.net mvc image path and virtual directoryasp.net mvc 镜像路径和虚拟目录
【发布时间】:2011-06-28 15:39:04
【问题描述】:

我知道这一定是重复的,但我一直在翻阅这方面的大量信息,但我无法让它发挥作用。

我正在尝试让一个站点在客户端的服务器上运行,并且他们将该站点安装在虚拟目录中。我在本地没有这个设置,所以我在这里瞎了眼。

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

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

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

输出:

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

我也试过了:

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

但这正在产生:

<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) 但它输出的是相对 url,而不是绝对的。

【问题讨论】:

  • 为什么不在您的开发环境中的虚拟目录中托管它,这样您就不必再盲目了

标签: c# asp.net asp.net-mvc-2


【解决方案1】:

这段代码

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 引用目录的情况下,因此您会得到默认页面(例如,@987654323 @ 可能是实际资源,在这种情况下 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://local-part@domain.com
    • 解析为mailto:local-part@domain.com

【讨论】:

  • +1 我发现这个答案非常有用并解决了我的问题。无论它在哪里运行,我都需要找到主路径(//localhost/MyApp/ 或//website/),这样我的 javascript 重定向才能工作。
  • 你能告诉我如何测试这个吗? (使用 UrlHelper)
【解决方案2】:

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

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)
        );
    }
}

然后:

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

将输出例如:

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

【讨论】:

    【解决方案3】:

    【讨论】:

      【解决方案4】:

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

      我个人喜欢关注best practices guide here到:

      创建 UrlHelper 的扩展方法 从 Route 生成您的 url

      您可能需要为您拥有的这些静态图像提供扩展方法:

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

      那么你可以这样做:

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

      【讨论】:

      • 我建议使用 MvcContrib 来检查“url”的编译时间
      • 我按照您的引用开始了这个最佳实践,但我的设置无法识别 .FormatWith()。会不会是我使用的是 MVC2 而那是 MVC1 的?
      猜你喜欢
      • 2010-11-29
      • 1970-01-01
      • 1970-01-01
      • 2022-11-23
      • 2011-06-15
      • 2010-10-11
      • 2011-02-26
      • 2010-09-16
      • 1970-01-01
      相关资源
      最近更新 更多