在asp.net mvc视图中有什么简单的(内置的)方法来获取文件在内容文件夹中的绝对路径吗?
目前我使用的是
@Url.Content("~/Content/images/logo.png")
但是返回的路径不是绝对的。
我知道为这种情况构建自己的助手是可能的,但我想知道是否有更简单的方法……
发布于 2014-06-01 16:58:00
这对我来说很有效:
帮助者:
using System;
using System.Web;
using System.Web.Mvc;
public static class UrlExtensions
{
public static string Content(this UrlHelper urlHelper, string contentPath, bool toAbsolute = false)
{
var path = urlHelper.Content(contentPath);
var url = new Uri(HttpContext.Current.Request.Url, path);
return toAbsolute ? url.AbsoluteUri : path;
}
}
cshtml中的用法:
@Url.Content("~/Scripts/flot/jquery.flot.menuBar.js", true)
// example output:
// http://example.com/directory/Scripts/flot/jquery.flot.menuBar.js
发布于 2014-08-25 21:55:13
这将生成图像(或文件)的绝对url。
Request.Url.Scheme + "://" + Request.Url.Authority + Url.Content("~/Content/images/logo.png")
这在Asp.net核心中有效
Context.Request.Scheme + "://" + Context.Request.Host + Url.Content("~/images/logo.png")
发布于 2013-04-24 19:14:29
Url.Content返回绝对路径。您需要的是域(和端口)。您可以通过以下方式获取当前域名:
Request.Url.Authority
然后将此字符串与图像的绝对路径字符串组合在一起。它将返回域名,如果您在不同的端口上,还将包括端口号。
https://stackoverflow.com/questions/16084081
复制相似问题