Sco*_*ott 2 c# asp.net asp.net-mvc-2
我知道这必须是重复的,但我一直在涉及这方面的大量信息,我无法让它工作.
我正在尝试让一个站点在客户端的服务器上运行,并且他们将站点安装在虚拟目录中.我本地没有这个设置,所以我在这里盲目飞行.
我正在尝试建立一个图像的路径.(这是Facebook OpenGraph元数据).
我需要图像的路径是一个完全合格的绝对URL.我尝试过很多东西,但似乎没什么用.下面的代码输出一个相对url,但这不起作用.
<% var urlHelper = VirtualPathUtility.ToAbsolute("~/static/images/image.jpg");%>
<meta property="og:image" content="<%=urlHelper%>" />
Run Code Online (Sandbox Code Playgroud)
输出:
<meta property="og:image" content="/static/images/image.jpg" />
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
<% var serverHost = HttpContext.Current.Request.Url; %>
<meta property="og:image"
content="<%=serverHost + "/static/images/image.jpg"%>" />
Run Code Online (Sandbox Code Playgroud)
但那产生了:
<meta property="og:image"
content="http://localhost:51863/ViewFile.aspx/static/images/image.jpg" />
Run Code Online (Sandbox Code Playgroud)
我正在寻找http://example.com/virtualdirectory/static/images/image.jpg
任何帮助都感激不尽.我真的不想对网址进行硬编码.
谢谢,斯科特
我忽略了提到我的第一次尝试是Url.Content("〜/ .... jpg)但是它输出了一个相对的url,而不是一个绝对的url.
Nic*_*rey 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
}
}
Run Code Online (Sandbox Code Playgroud)
应该适用于你可以抛出的任何URI.
但有一点需要注意:页面相对URI(例如foo/bar/image.png)可能无法解析您认为的方式,特别是如果URI引用目录,那么您将获得默认页面(即,http://localhost/foo/bar可能是实际资源,其中如果URI完整,或者它可能是不完整的,在这种情况下,Asp.Net MVC的路由填充空白.所有请求都是原始URI.如果你想修复它,你需要得到RouteData的请求并询问详细信息.
以下是http://localhost/MyApp从控制器About视图以不同方式植入和调用Html帮助方法的Web应用程序解决问题的方法Home.
~
http://localhost/MyApp//
~/
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.pnghttp://somesite.com/foo/bar/myImage.png
http://somesite.come/foo/bar/myImage.pnghttp://somesite.com:123/foo/bar/myImage.png
http://somesite.come:123/foo/bar/myImage.pngftp://somesite.com/foo/bar/myImage.png
ftp://somesite.come:123/foo/bar/myImage.pngmailto://local-part@domain.com
mailto:local-part@domain.com你可以写一个小的扩展方法:
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)
);
}
}
Run Code Online (Sandbox Code Playgroud)
然后:
<meta property="og:image" content="<%= Url.AbsoluteContent("~/static/images/image.jpg") %>" />
Run Code Online (Sandbox Code Playgroud)
将输出例如:
<meta property="og:image" content="http://localhost:7864/static/images/image.jpg" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14115 次 |
| 最近记录: |