MVC3:什么是让从物体的HtmlHelper视图路径的最佳方法? [英] MVC3: What is the best way to get the view path from an HtmlHelper object?
本文介绍了MVC3:什么是让从物体的HtmlHelper视图路径的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个HTML扩展方法来检索URL为位于同一文件夹视图中的文件。
示例
/Views/Home/Index.cshtml
/Views/Home/Index.js
/Views/Home/Index.css
这是做到这一点的最好方法是什么?我不喜欢它,因为我必须做到以下几点演员。我相信,RazorView不会,如果你使用的是不同的视图引擎,但只IVIEW上有一个Render方法工作。
((RazorView)helper.ViewContext.View).ViewPath
下面是完整的方法
公共静态字符串的getURL(这System.Web.Mvc.HtmlHelper帮手,字符串文件名){
字符串virtualPath =((RazorView)helper.ViewContext.View).ViewPath;
virtualPath = virtualPath.Substring(0,virtualPath.LastIndexOf(/)+ 1);
返回virtualPath +文件名;
}
解决方案
如果您使用的Razor视图引擎
,您可以使用此:
((System.Web.Mvc.RazorView)htmlHelper.ViewContext.View).ViewPath
好运气
I have an html extension method to retrieve the URL for a file that is located in the same folder as the view.
Example
/Views/Home/Index.cshtml
/Views/Home/Index.js
/Views/Home/Index.css
Is this the best way to do this? I don't like it because I have to do the following cast. I am sure that RazorView is not going to work if you are using a different view engine, but IView only has a Render method on it.
((RazorView)helper.ViewContext.View).ViewPath
Here is the full method
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;
}
解决方案
If you use Razor view engine
, You can use this:
((System.Web.Mvc.RazorView)htmlHelper.ViewContext.View).ViewPath
Good luck
这篇关于MVC3:什么是让从物体的HtmlHelper视图路径的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文