asp.net - MVC3 : What is the best way to get the view path from an HtmlHelper object?

标签 asp.net asp.net-mvc-3 razor

我有一个 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/

相关文章:

asp.net - 在正则表达式中允许-(破折号)

asp.net-mvc - Razor 页面中的多语言文本

asp.net - 无法使用 css pseudo :before on class of ASP. NET 自定义 Web 控件

javascript - 代码可以在 IE 中运行,但不能在 FF 中运行

asp.net-mvc - 在Webgrid中将 bool 值显示为复选框

asp.net - MVC 4 PartialViewResult 缓存 jQuery $.post

asp.net-mvc-3 - 更改 VS 2010 中的默认 View

Html.BeginForm 内部的 Html.BeginForm MVC3

javascript - 如何使用 javascript 获取复选框属性?

c# - 使用 ASP.NET 在 Windows Azure Blob 存储上设置 CORS