Take the 2-minute tour ×

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.

share|improve this question
If you like 3rd way, you can use it without controllers with same name. You can specify naming conventions for controllers (i.e. BlogController, AdminBlogController). And then write custom ControllerFactory. It will see that there is /admin/ root and will concatenate it with the next root in order to find out the name of controller to create. So, if user types "/site/admin/blog/edit" ControllerFactory will just use AdminBlogController. –  Egor4eg Aug 4 '11 at 15:01
Thanks, although I don't know anything about ControllerFactory, I will try to find something about it now. :) –  Steve Aug 4 '11 at 15:10
add comment

3 Answers

up vote 3 down vote accepted

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.

share|improve this answer
But if I create same controller name in Admin area I still get The request for 'Home' has found the following matching controllers: proj.Controllers.HomeController proj.Areas.Admin.Controllers.HomeController –  Steve Aug 4 '11 at 15:02
Why would you use the same name for your controllers? One is an admin controller, the other not :) @Egor4eg's sugestion is a good one. Go with AdminBlogController for the admin stuff. –  Jamie Dixon Aug 4 '11 at 15:03
It's perfectly valid to have two controllers with the same name. You would need to restrict the default route's namespaces to search in your global.asax (5th parameter of the MapRoute method; you can set the 4th parameter, the route constraints, to null). –  Marius Schulz Aug 4 '11 at 15:09
add comment

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.

share|improve this answer
add comment

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 .

share|improve this answer
Yes, this is one of the options. Only one thing is what bugs me here is the URL itself. I would like it to be site/admin/blog/edit/id(or name). If I don't come up with something I will just use this method. –  Steve Aug 4 '11 at 15:21
add comment

Your Answer


