mvc3:建议控制器结构
我正开始习惯 MVC,经过几个小学习项目后,我现在准备好接受更大的挑战了。
我正在寻找这样的结构。
/管理/索引
/admin/user/create:读取:更新:删除:列表
/admin/news/create:read:update:delete:list
鉴于最后两个,我认为我应该有控制器
管理员
管理员用户
AdminNews
...我应该将视图存储在这些文件夹中
/视图/管理
/视图/管理员/用户
/Views/Admin/News
上面的内容听起来不错吗?
最后,我如何设置这些路由来访问这些控制器?
我尝试了类似的方法,但没有成功。
routes.MapRoute(
"Admin/User", // Route name
"/Admin/{controller}/{action}/{id}", // URL with parameters
new {controller = "AdminUser", action = "Index", id = UrlParameter.Optional} // Parameter defaults
);
I am staring to get used to MVC and after a couple little learning projects, I am now ready to take a larger bite.
I am looking to work on a structure like this.
/admin/index
/admin/user/create:read:update:delete:list
/admin/news/create:read:update:delete:list
Given the last two, I am thinking that I should have controllers
Admin
AdminUser
AdminNews
... and that i should have views stored in these folders
/Views/Admin
/Views/Admin/User
/Views/Admin/News
Does the above sound ok?
Finally, how do i set up those routes to hit those controllers?
I tried something like this which did not work.
routes.MapRoute(
"Admin/User", // Route name
"/Admin/{controller}/{action}/{id}", // URL with parameters
new {controller = "AdminUser", action = "Index", id = UrlParameter.Optional} // Parameter defaults
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您开始学习 asp.net mvc 并尝试实现管理面板,我建议您选择“Areas”。查看演练:组织 ASP.NET使用区域的 MVC 应用程序。
If you are starting to learn asp.net mvc , and trying to implement admin panel, i would suggest "Areas". Take a look at Walkthrough: Organizing an ASP.NET MVC Application using Areas.
我会使用区域功能。
看到这个:
让我想到您可以添加一个管理区域,并在该区域下有单独的控制器。那么你的 URL 将是 /Admin/User 和 /Admin/News 等。
I would use the areas feature.
Seeing this:
Leads me to think that you could add an Admin area and have separate controllers underneath that area. Then your Urls would be /Admin/User and /Admin/News etc.
我同意区域可能是您可能想要研究的东西,因为它对于管理类型区域来说是理想的,并且网络上有大量的示例。
但是,如果您不走那条路,那么就稍微回答一下您的问题。
第一的。视图将位于文件夹中:
其次。该路线只需是项目中首次设置的默认路线即可
即
,其中的操作将是从 Admin、AdminUser 和 AdminNew 控制器公开的方法。
要获得您提到的路由,您可以执行以下操作:
对于新闻也是如此。对于管理员来说,我相信默认路由会捕捉到这一点。
希望有帮助。
I agree that areas might be something you may want to look into as it is ideal for an admin type area and there are tonnes of examples for it in the web.
However if you were not going down that route, then to answer your question somewhat.
First. The views would be in folders:
Second. The route need simply be the default route that is first setup in the project
i.e
where the action will be the methods exposed from Admin, AdminUser and AdminNew controllers.
To have the routing you mentioned you could do something like:
And likewise for news. For Admin I believe the default route would catch that.
Hope that helps.
您还可以使用一个区域作为管理部分。
You could also use an area for the admin part.