Valamas
Valamas

Reputation: 24719

mvc3 : Suggest controller structure

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
    );

Upvotes: 0

Views: 484

Answers (4)

Fatih Türker
Fatih Türker

Reputation: 135

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.

Upvotes: 1

dreza
dreza

Reputation: 3645

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.

Upvotes: 1

Mikael Östberg
Mikael Östberg

Reputation: 17146

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.

Upvotes: 1

Tobias
Tobias

Reputation: 2985

You could also use an area for the admin part.

Upvotes: 0

Related Questions