tags:

views:

499

answers:

3

How can I do in ASP.NET MVC, to take sub folders. For example, taking the following folder structure on the controller:

/Controller
  /Blog
     ViewsController.cs
     ArticlesController.cs
  /Customers
     SalesController.cs
     ProductsController.cs
  HomeController.cs

I would like to have the following folder structure in view, each view found your controller:

/Views
  /Blog
     /Views
        Index.aspx
        Admin.aspx
        Show.aspx
     /Articles
        Show.aspx
        Admin.aspx
  /Customers
     /Sales
        Index.aspx
        Totals.aspx
     /Products
        Index.aspx
        Promotions.aspx
  /Home
     Index.aspx
+5  A: 

This is a feature that has been added in ASP.NET MVC 2.0. It is called Areas.

Darin Dimitrov
it actually shows the answer there how to do it in MVC1 too I believe by head.
bastijn
Sorry, I should have clarified first that I am working with "MVC1. Is there any solution for "MVC1"
andres descalzo
+1  A: 

You could do it using Routes, i.e.

routes.MapAreaRoute("Blogs", 
        "Blog/Views/{controller}/{action}/{id}", 
        new { controller = "Views", action = "Index", id = "" });

That would seem to meet your needs given the data above.

Lazarus
Note that MapAreaRoute has been removed from MVC 2.0, and has been implemented differently now. There's an article in Swedish here that you could run through Google Translate. http://weblogs.asp.net/mikaelsoderstrom/archive/2009/10/02/areor-i-asp-net-mvc-2.aspx.
Jan Aagaard
+1  A: 

As Darin mentioned, Areas seem to be the "intended" way for developers to accomplish this. If you can wait until February, you might consider using the MVC 2 preview. However, developers were doing similar things before the introduction of Areas with MVC 2. If you need a more immediate solution, you can put your controllers in the folder structure you specified above. Assuming the controllers are namespaced according to their folder (i.e. Project.Controllers.Blog), you simply add an extra parameter to the end that specifies the namespace for the controller when you initialize your routes.

For example:

routes.MapAreaRoute("Blog", 
    "Blog/{controller}/{action}/{id}", 
    new { controller = "Articles", action = "Index", id = "" },
    "Project.Controllers.Blog");
Brad Gignac