开发者

ASP.NET MVC Controller design

I have a question about开发者_运维问答 how to design my controllers properly.

Project is fairly simple: blog, immage gallery with categories and images inside them, news secion.

However I don't know how to organize my controllers for this because I want to make somewhat admin panel where administrators can edit, add and modify these things. I've came up with 3 scenariosu so far...

  1. Admin panel will have links to site/controller/edit, but layout for these action results will be different from standard one.

  2. Admin controller will have all these actions like BlogAdd, BlogEdit so that url will be something like /site/admin/blogedit.

  3. Create copies of Blog controller in admin folder so url will be like /site/admin/blog/edit - i sense problems with routing since 2 controllers with same name does not sound like a good idea, however I like ho URL looks in this situation.

What I'm trying to make is CMS somewhat similar to wordpress, where blog creation,editing and deletion is completely separated from default blog itself.


I suggest you stop thinking about URLs and Controllers being a 1->1 relationship. This will make things MUCH easier and less confusing. You can make the URLs work however you want with MVC's routing mechanism and there's no reason to restrict your controller design/organization because of the URLs you want, because you can always adapt the routing to with with the URLs you have in mind.

While building the website, just focus on the controllers (and the general interface) and ignore the URLs until you get to that point, and then when you come up with a good URL scheme go into the routing system and add the routes to connect to your existing controller actions as you want.

Once you code out your blogging engine you will have a much better idea of the user workflow and will probably find different ways to organize your URLs, and you can then reorganize your URLs without touching the controllers themselves.

As to your first requirement:

There are two ways to do this depending on your end goal. If your goal is to display the same core content, but have different user options available (different overall layout, additional buttons on the page, etc..) then the best idea is really to just pass in an IsAdministrator property in your view model, and make the slight changes to the page based on if that's true or false. The reason is because you still (most likely) want the core of the page to be the same, and this keeps you from duplicating code that is related to the core data (data that's being displayed for both admins and non-admins).

Edit: So in summary, organize your controllers based on what makes it easier to develop with, not based on how the user interacts with the system with. You can always change the latter, changing the former is harder and will make maintenance annoying.


You can create Areas in your MVC project and have your admin functionality in a controller in your admin area.

This will allow you to easily seperate your administration functionality from your general blog functionality.

That's how I'd do it.


Why don't you keep the routes the same and handle the different roles via security? For example:

  • /blog/name-of-topic/view to view a topic (all users)
  • /blog/name-of-topic/edit to edit a topic (only enabled for logged in users)
  • /blog/add to create new topics (only enabled for logged in users)

You can handle these actions in a single controller and decorate the actions that require logged users via the [Authorize] attribute. Same thing with the links on your views, you would enable the links to edit and add topics only to visible users.

You could still have a separate panel to allow admins to hit the aforementioned add/edit links .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜