我正在尝试获取图像的完整路径。我的第一次尝试是:
<img alt="" src="@Url.Content("~/Uploads/Logos/" + Model.ImageName + ".png")" />
这会产生:
<img alt="" src="/Uploads/Logos/Image1.png" />
哪个不够好。我真的需要完整的URL,如:
<img alt="" src="http://localhost/Uploads/Logos/Image1.png" />
或:
<img alt="" src="http://www.liveserver.com/Uploads/Logos/Image1.png" />
因此需要添加http和服务器位。
方法是什么?
提前致谢。
答案 0 :(得分:2)
尝试为此创建一个HTML帮助扩展:
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;
}
}
然后像这样使用它:
<img alt="" src='@Html.FullyQualifiedUri("~/Uploads/Logos/" + Model.ImageName + ".png")' />