添加子目录到"查看/共享"夹在ASP.Net MVC和调用视图 [英] Adding sub-directory to "View/Shared" folder in ASP.Net MVC and calling the view
问题描述
我目前正在开发使用ASP.Net MVC3剃刀的网站。里面的查看/共享文件夹,我想添加一个名为局部变量在那里我可以把我所有的局部视图(组织网站更好的缘故子文件夹。
I'm currently developing a site using ASP.Net MVC3 with Razor. Inside the "View/Shared" folder, I want to add a subfolder called "Partials" where I can place all of my partial views (for the sake of organizing the site better.
我可以,只要我总是引用调用的意见时,局部变量文件夹,这样做没有问题(用剃刀):
I can do this without a problem as long as I always reference the "Partials" folder when calling the views (using Razor):
@Html.Partial("Partials/{ViewName}")
我的问题是,如果有一种方法来局部变量文件夹添加和.NET经过一个视图搜索时的列表中,这样,我可以叫我的看法,而不必引用局部变量文件夹中,如所以:
My question is if there is a way to add the "Partials" folder to the list that .Net goes through when searching for a view, this way I can call my view without having to reference the "Partials" folder, like so:
@Html.Partial("{ViewName}")
感谢您的帮助!
推荐答案
解决了这个。要尝试使用定位在剃刀局部视图时添加共享/局部变量子我创建的位置列表目录中搜索:
Solved this. To add the "Shared/Partials" sub directory I created to the list of locations searched when trying to locate a Partial View in Razor using:
@Html.Partial("{NameOfView}")
首先创建一个RazorViewEngine视图引擎为它的基类,并添加视图位置如下。同样,我想我所有的局部视图存储在我默认的查看/共享的MVC创建的目录。
First create a view engine with RazorViewEngine as its base class and add your view locations as follows. Again, I wanted to store all of my partial views in a "Partials" subdirectory that I created within the default "Views/Shared" directory created by MVC.
public class RDDBViewEngine : RazorViewEngine
{
private static string[] NewPartialViewFormats = new[] {
"~/Views/{1}/Partials/{0}.cshtml",
"~/Views/Shared/Partials/{0}.cshtml"
};
public RDDBViewEngine()
{
base.PartialViewLocationFormats = base.PartialViewLocationFormats.Union(NewPartialViewFormats).ToArray();
}
}
注意,{1}中的位置格式是控制器名称和{0}的视图的名称
Note that {1} in the location format is the Controller name and {0} is the name of the view.
然后该视图引擎添加到MVC ViewEngines.Engines集合在Application_Start()方法在Global.asax中:
Then add that view engine to the MVC ViewEngines.Engines Collection in the Application_Start() method in your global.asax:
ViewEngines.Engines.Add(new RDDBViewEngine());
这篇关于添加子目录到"查看/共享"夹在ASP.Net MVC和调用视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!