我有一个 html 扩展方法来检索与 View 位于同一文件夹中的文件的 URL。
示例
/Views/Home/Index.cshtml
/Views/Home/Index.js
/Views/Home/Index.css
这是最好的方法吗?我不喜欢它,因为我必须做以下 Actor 。如果您使用不同的 View 引擎,我确信 RazorView 不会工作,但 IView 上只有一个 Render 方法。
((RazorView)helper.ViewContext.View).ViewPath
这是完整的方法
public static string GetUrl(this System.Web.Mvc.HtmlHelper helper, string fileName) {
string virtualPath = ((RazorView) helper.ViewContext.View).ViewPath;
virtualPath = virtualPath.Substring(0, virtualPath.LastIndexOf("/") + 1);
return virtualPath + fileName;
}
最佳答案
如果您使用 Razor view engine
,你可以用这个:
((System.Web.Mvc.RazorView)htmlHelper.ViewContext.View).ViewPath
祝你好运
关于asp.net - MVC3 : What is the best way to get the view path from an HtmlHelper object?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7058349/