Tutorial :ASP.Net MVC: File Structure, Controllers, and Views: How do I separate admin from rest?



Question:

Basically how can I get this:

sitename/Admin/Forum/Index versus sitename/Forum/Index

Background: To learn MVC I've taken on creating a cheesy forum application. Before knowing anything about MVC I just jumped right in with the default director structure, basically this:

IMGAE!!1 http://byatool.com/wp-content/uploads/2009/06/stack-projectshot.jpg

Now all these controllers/views deal with admin side things. This was all fine and dandy until I started to work on the non admin side stuff. Now I'm at a loss on how to transport the current setup to an Admin and Non Admin capable site. Basically as I typed up before:

sitename/Admin/Forum/Index - For Admin

sitename/Forum/Index - For regular users

As you can see, the Forum name, and therefore the Forum controller would overlap and MVC doesn't much like this.

I'd really not like to have a siteName/Forum/AdminIndex or siteName/ForumAdmin/Index i.e. ForumAdminController because that seems like an odd work around and wouldn't mind having a check against the users at the Admin/ level to disallow anyone getting in to anything in that "folder" (lack of better word) without being an admin. (I assume this would be done through the route)

I'm really hoping there's a way to pull this off.


Solution:1

I think that your admin or console web application should always go into its own project. This way you can have more control if ever it is to be deployed to an intranet setting, if you need to hide certain functionality from your main site, etc. Co-mingling these two concerns under one site rarely ends up being a good idea as the site grows and grows. Also, an admin site rarely benefits from the design and so on of the main application. Administrative functions usually benefit from a more direct layout, additional screens, more security, etc.

Think about http://console.sitename.com/forum/index. This is much clearer and can still have ties to your forum application. The concerns are very much separated here.


Solution:2

The beauty of MVC is that your URL's don't tie to static resources like they do in ASP.NET Webforms. With that in mind, you can craft your Admin URL's to look however you like and point at whatever controller you like rather than creating some intricate directory structure. In the past, putting Administrative functions within a subfolder was beneficial because you could lock that folder down easily with configuration files, but that isn't needed now that Views are separate from URL's.

Typically I handle things by just configuring my Admin routes the way I'd like them and pointing them at an Administrative controller. The controller is then locked down to only users in an Administrative role using an Authorize decorator on the controller class. Then it's just a matter of making sure people are in the correct roles. My URL's are usually something to the effect of "Admin/ManageUsers." Maybe there are some shortcomings of this, but I haven't run into any issues thus far.


Solution:3

I've done both alternatives Bit and Andrew present and i think in this case i've put the admin stuff in a separate Area or bundle the views together in an admin folder for better overview. On the other hand, Bit's alternative is very good when you reuse the views in both the forum and the admin site but with slightly different functionality depending on authorization rules.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »