asp.net mvc的映像路径和虚拟目录 [英] asp.net mvc image path and virtual directory
问题描述
我知道这已经是重复的,但我一直在通过的相关信息,成群的涉水,我无法得到它的工作。
I know this has to be a duplicate, but I've been wading through the hordes of information on this and I can't get it work.
我试图让网站客户端的服务器上工作,他们有安装在虚拟目录的站点。我没有这个设置,在本地,所以我在这里盲目。
I'm trying to get a site working on a client's server and they have the site installed in a virtual directory. I don't have this setup, locally, so I'm flying blind here.
我试图建立到图像的路径。 (这是对于Facebook OpenGraph元数据)。
I'm trying to build a path to an image. (it's for Facebook OpenGraph Meta data).
我需要的路径,图像是完全合格的,绝对URL。我试过很多东西,但似乎没有任何工作。下面的code输出一个相对URL,但是这不会起作用。
I need the path to the image to be a fully-qualified, absolute url. I've tried so many things, but nothing seems to work. The code below outputs a relative url, but this will not work.
<% 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
任何帮助将非常AP preciated。我真的不希望有硬code中的网址。
Any help will be much appreciated. I really don't want to have to hard-code the url.
谢谢,
斯科特
Thanks, Scott
我忘了提,我的第一个尝试是Url.Content(〜/ .... JPG),但输出相对URL,而不是abosolute之一。
I neglected to mention that my very first attempt was Url.Content("~/....jpg) but that outputs a relative url, not an abosolute one.
推荐答案
您可以写一个小的扩展方法:
You could write a small extension method:
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" />
这篇关于asp.net mvc的映像路径和虚拟目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!