如何指定为 Mvc 控制器创建 Mvc 视图的位置?
-
03-07-2019 - |
题
重要更新
自 MVC 2.0 Preview 1 发布以来,此功能已以区域的形式作为实际框架本身的一部分实现。更多详细信息请访问 Phil Haack 的博客 这里
我有一个名为 ListManagerController 的控制器。该控制器包含一个名为 Index() 的 ActionResult 方法。当我在 Visual Studio 中右键单击“索引”并选择“添加视图”时,将在 /Views/ListManager/Index 中创建新视图。
但是我希望在 /Views/ 中创建索引视图和所有后续视图管理/列表管理器/。我将如何实现这个目标?
编辑:有人指出这个问题与发布的问题重复 这里. 。看来我的搜索技巧一开始就失败了。
其他提示
我知道你已经接受了答案,但这是我在尝试同样的想法时想出来的,在 Phil Haack的帖子。
首先,您需要拥有自己的ViewEngine来查找View文件夹下的文件夹。这样的事情:(你会注意到它看起来很像Phil Haack的区号)
public class TestViewEngine : WebFormViewEngine
{
public TestViewEngine()
: base()
{
MasterLocationFormats = new[] {
"~/Views/{1}/{0}.master",
"~/Views/Shared/{0}.master"
};
ViewLocationFormats = new[] {
"~/{0}.aspx",
"~/{0}.ascx",
"~/Views/{1}/{0}.aspx",
"~/Views/{1}/{0}.ascx",
"~/Views/Shared/{0}.aspx",
"~/Views/Shared/{0}.ascx"
};
PartialViewLocationFormats = ViewLocationFormats;
}
public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
{
ViewEngineResult rootResult = null;
//if the route data has a root value defined when mapping routes in global.asax
if (controllerContext.RouteData.Values.ContainsKey("root")) {
//then try to find the view in the folder name defined in that route
string rootViewName = FormatViewName(controllerContext, viewName);
rootResult = base.FindView(controllerContext, rootViewName, masterName, useCache);
if (rootResult != null && rootResult.View != null) {
return rootResult;
}
//same if it's a shared view
string sharedRootViewName = FormatSharedViewName(controllerContext, viewName);
rootResult = base.FindView(controllerContext, sharedRootViewName, masterName, useCache);
if (rootResult != null && rootResult.View != null) {
return rootResult;
}
}
//if not let the base handle it
return base.FindView(controllerContext, viewName, masterName, useCache);
}
private static string FormatViewName(ControllerContext controllerContext, string viewName) {
string controllerName = controllerContext.RouteData.GetRequiredString("controller");
string root = controllerContext.RouteData.Values["root"].ToString();
return "Views/" + root + "/" + controllerName + "/" + viewName;
}
private static string FormatSharedViewName(ControllerContext controllerContext, string viewName) {
string root = controllerContext.RouteData.Values["root"].ToString();
return "Views/" + root + "/Shared/" + viewName;
}
}
然后在 Global.asax
中用 Application_Start
替换默认的ViewEngine和自定义的ViewEngine:
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new TestViewEngine());
现在,当您在 Global.asax
中定义路由时,需要设置一个 root
值,指示要在View文件夹下查找的文件夹,如下所示:
routes.MapRoute(
"ListManager",
"ListManager/{action}/{id}",
new { controller = "ListManager", action = "Index", id = "", root = "Manage" }
);
这个问题很大程度上是重复的 这个问题
所以我会在这里引用我对此问题的回答。
我想出了一个不同的解决方案 这不需要我自己滚动 视图引擎。
基本上,我想将 MVC 保留为 “约定”尽可能驱动,但我 仍然想组织我所有的 ~/Views/Admin 下的 “Admin” 视图 文件夹。
例子:
- 〜/视图/管理员/用户/
- 〜/视图/管理/新闻/
- 〜/视图/管理/博客/
我的解决方案是创建一个新的基地 我的特定管理员的类 控制器并“强制”路径 该控制器的视图。
我有一篇博文和示例代码 这里: 在 ASP.Net 中组织视图 MVC的