使用管理子站点进行路由的正确流程
我正在构建我的第一个 Asp.Net MVC2 站点,现在我正在尝试向该站点添加 /Admin 区域。
我不希望主要用户组看到此区域,因此只有当您输入 http://Intranet/Admin 时才能访问该区域
我拥有的是一个供普通用户使用的 NewsController,但我还想要一个管理 NewsController,并且我不确定如何设置类层次结构和文件夹,以便当我添加视图时它们位于正确的位置。
在我的 Global.Asax.cs 中,我添加了并且路由解析正确。
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "Intranet.Controllers" }
);
routes.MapRoute(
"Admin", // Route name
"Admin/{controller}/{action}/{id}", // URL with parameters
new { controller = "Admin", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "Intranet.Controllers.Admin" }
);
在我
Views/
Admin/
News/
...I want the new view to go here...
在控制器中
Controllers/
Admin/
AdminController.cs
NewsController.cs (this is the one i want for administration)
NewsController.cs (this is the regular one for viewing the list, specific item etc)
设置的文件夹层次结构中,我面临的问题是当我进入索引上的 admin/NewsController.cs 并添加视图时,它尝试在 /News/Index.aspx 而不是 /Admin/ 创建它新闻/Index.aspx。
这是我的管理新闻控制器的代码 Controllers/Admin->Add->Controller
namespace Intranet.Controllers.Admin
{
public class NewsController : Controller
{
public ActionResult Index()
{
return View();
}
}
}
是否有什么我做错了,或者我应该更改什么,以便当我添加视图时它们将在 /Admin/ 中创建{区域}目录。
I'm building my first Asp.Net MVC2 Site, and i'm now trying to add an /Admin area to the site.
I don't want this area to be visibile to the main set of users so will only be accessible when you enter http://Intranet/Admin
What I have is a NewsController for my regular users but I also want an Admin NewsController and I'm not sure how to setup the Class hierarchy and folders so that when I add the Views they are in the correct location.
Inside my Global.Asax.cs I've added and the routes resolve correctly.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "Intranet.Controllers" }
);
routes.MapRoute(
"Admin", // Route name
"Admin/{controller}/{action}/{id}", // URL with parameters
new { controller = "Admin", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "Intranet.Controllers.Admin" }
);
And in the folder hierarchy I've setup
Views/
Admin/
News/
...I want the new view to go here...
In the Controllers
Controllers/
Admin/
AdminController.cs
NewsController.cs (this is the one i want for administration)
NewsController.cs (this is the regular one for viewing the list, specific item etc)
The problem I face is when I go into the admin/NewsController.cs on Index and Add View it tries to create it at the /News/Index.aspx rather than /Admin/News/Index.aspx.
This is the code for my admin news controller Controllers/Admin->Add->Controller
namespace Intranet.Controllers.Admin
{
public class NewsController : Controller
{
public ActionResult Index()
{
return View();
}
}
}
Is there something I'm doing incorrectly, or what should I change so that when I add the views they are being created in the /Admin/{area} directory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您使用的是 MVC2,解决此问题的最简单方法是为您的管理部分创建一个实际的 MVC“区域”。现在,您正在默认部分中执行所有操作,并且仅使用管理文件夹。如果您创建一个管理区域(在众所周知的位置区域下)文件夹,那么您将拥有一个 AdminAreaRegistration - 您将在其中配置管理路由。因为您将作为区域的一部分执行此操作,所以 URL“/Admin”的第一段将用于“区域”令牌。这将消除要使用哪个控制器的歧义并正确选择您想要的控制器。所以你的文件夹结构将是:
Since you're using MVC2, the easiest way to solve this is the create an actual MVC "Area" for your Admin section. Right now you're doing everything in the default section and just using an Admin folder. If you create an Admin area (under the well-known location Areas) folder, then you'll have an AdminAreaRegistration - where is where you'll configure your Admin routes. Because you'll do this as part of the Area, then the first segment of the URL "/Admin" will be used for the "area" token. This will disambiguate which controller to use and correctly pick up the controller you want. So you're folder structure will be:
当您尝试为现有控制器操作创建视图时,它始终会在视图的根文件夹中创建。视图的默认路由始终指向视图文件夹的根目录。
例如:
在该层次结构中,Admin 和 root 内的 HomeController 共享视图文件夹中的相同视图。
除非您在控制器的管理文件夹内的 HomeController 的所有 ActionResults 中返回指定的 View() 。它将映射到某个视图。
例如,Controllers 中 Admin 文件夹的 HomeController.cs 内的 ActionResult。
这将像这样映射到 Views 文件夹中
,但是如果您在 ActionResult 中返回视图时未指定视图路径,它将映射到视图的默认位置,如下所示。
这样做的原因是,即使您在 Global.asax 中指定了路由,也只是映射 url 应指向的控制器,而不是 Views 文件夹。
当您右键单击并在控制器的任何子级别的 ActionResult 上创建视图时,它始终会在 Views 文件夹的根目录中创建其相应的控制器。
When you try to create a View for the existing Controller Action it always create on the root folder of the Views. The default route for the View is always pointing to the root of the Views folder.
For example:
In that hierarchy, both of the HomeController inside Admin and root shares the same Views in the Views Folder.
Unless you return a specified View() in all of the ActionResults in your HomeController inside the Admin Folder of your Controllers. It will map to a certain View.
Example, ActionResult inside the HomeController.cs of Admin folder in Controllers.
This will be mapped in the Views folder like this
But if you do not specify the View path when you return a View in your ActionResult it will map to the default location of the Views which is like this.
The reason for this is that even though you specify the routes in the Global.asax, that is only to map to which controller the url should point, not the Views folder.
When you right click and Create View on ActionResult of any Sublevels of the Controllers, it always create on the root of the Views folder to its corresponding Controller.