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(例如
这是如何使用植根于
-
~ -
解析为
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] -
解析为
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" /> |
在
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") %>" /> |