mvc3:建议控制器结构

发布于 2024-11-17 17:30:46 字数 616 浏览 3 评论 0原文

我正开始习惯 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

红颜悴 2024-11-24 17:30:46

我正开始习惯 MVC,经过几个小学习项目后,我现在准备好接受更大的挑战了。

如果您开始学习 asp.net mvc 并尝试实现管理面板,我建议您选择“Areas”。查看演练:组织 ASP.NET使用区域的 MVC 应用程序

I am staring to get used to MVC and after a couple little learning projects, I am now ready to take a larger bite.

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.

数理化全能战士 2024-11-24 17:30:46

我会使用区域功能。

看到这个:

  • Admin
  • AdminUser
  • AdminNews

让我想到您可以添加一个管理区域,并在该区域下有单独的控制器。那么你的 URL 将是 /Admin/User 和 /Admin/News 等。

I would use the areas feature.

Seeing this:

  • Admin
  • AdminUser
  • AdminNews

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.

千年*琉璃梦 2024-11-24 17:30:46

我同意区域可能是您可能想要研究的东西,因为它对于管理类型区域来说是理想的,并且网络上有大量的示例。

但是,如果您不走那条路,那么就稍微回答一下您的问题。

第一的。视图将位于文件夹中:

Views/Admin
Views/AdminUser
Views/AdminNew

其次。该路线只需是项目中首次设置的默认路线即可

{controller}/{action}/{id}

,其中的操作将是从 Admin、AdminUser 和 AdminNew 控制器公开的方法。

要获得您提到的路由,您可以执行以下操作:

routes.MapRoute(
"AdminUser",
/Admin/User/{action},{id},
new {controller = "AdminUser", action = "Index", id = UrlParameter.Optional}
);

对于新闻也是如此。对于管理员来说,我相信默认路由会捕捉到这一点。

希望有帮助。

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:

Views/Admin
Views/AdminUser
Views/AdminNew

Second. The route need simply be the default route that is first setup in the project
i.e

{controller}/{action}/{id}

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:

routes.MapRoute(
"AdminUser",
/Admin/User/{action},{id},
new {controller = "AdminUser", action = "Index", id = UrlParameter.Optional}
);

And likewise for news. For Admin I believe the default route would catch that.

Hope that helps.

泡沫很甜 2024-11-24 17:30:46

您还可以使用一个区域作为管理部分。

You could also use an area for the admin part.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文