views:

721

answers:

4

Can the folders within the View have subfolders? if so, how does the Controller reach them? For example... I would like to have the following urls work correctly:

  • Admin/Index
  • Admin/Profile/Index
  • Admin/Profile/Edit/1
  • Admin/Group/Index
  • Admin/Group/Edit/1

So, would I create a folder for Profile and Group as a subfolder within Admin?

I tried this and to map a route in the Global file but that does not seem to be working.

+1  A: 

Just make the return action be like this:

return View("Profile/Index");

That will make the trick for the controller find the "subview". For mapping the URLs to the right controller you can use the routing system, but ignore the subview, just map to the right controller and let the controller handle the correct view.

Augusto Radtke
this is actually a very good answer.. +1 :)
Ropstah
+3  A: 

Seems like you're trying to create admin panel and by putting other controller related actions into admin controller. I believe it's not the most efficient way. You could simply do like this:

Use RedirectToAction as return if needed for admin links

  • Admin/Index
  • Admin/Profile
  • Admin/Group

And put rest to appropriate controllers

  • Profile/Index
  • Profile/Edit/1
  • Group/Index
  • Group/Edit/1

Also add [Authorize(Roles = "Administrator")] and [AcceptVerbs(HttpVerbs.Post)] for create/edit/delete actions

Paul G.
A: 

There's another option that is using the concept of Areas in order to achieve a global grouping of views and controllers as described in this interesting Billy McCafferty's post:

Creating MVC "Areas" as Subfolders under Views

The code is available so you can check out how everything is done, but basically replaces the ViewEngine with another, similar to the original but with different management of the view locations and registers routes according to the areas defined.

Marc Climent
+2  A: 

You could create your own ViewEngine and override where to look for the files something like this

public class MyViewEngine : WebFormViewEngine
{
    public MyViewEngine()
    {
        ViewLocationFormats = new[] { 
            "~/{0}.aspx",
            "~/{0}.ascx",
            "~/Views/{1}/{0}.aspx",
            "~/Views/{1}/{0}.ascx",
            "~/Views/Shared/{0}.aspx",
            "~/Views/Shared/{0}.ascx",
        };

        MasterLocationFormats = new[] {
            "~/{0}.master",
            "~/Shared/{0}.master",
            "~/Views/{1}/{0}.master",
            "~/Views/Shared/{0}.master",
        };
        PartialViewLocationFormats = ViewLocationFormats;

    }
}

Then in your application start add

ViewEngines.Engines.Add(new MyViewEngine());
Remmus
this is good info... when I get more time I definitely want to look into this.
Eric Brown
This is exactly what I was looking for, thank you the simple example!!!Btw, if you can get get a "theme" var in there somehow, you could implement dynamic theme folders with this too... :)
Jason