Search code examples
.netasp.net-mvcasp.net-mvc-routingdirectory-structure

Create sub folders in the controller without Areas


I am developing a MVC 3 Web app and I want to create something like this:

/Controller
      /Blog
         BogController.cs
         ViewsController.cs
         ArticlesController.cs
      /Customers
         SalesController.cs
         ProductsController.cs          
      HomeController.cs
/Views
     /Blog
        Index.aspx
        Summary.aspx
        /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

Create sub folders in the controller

But in the solution that they answer to this guy was for MVC 2 and in MVC 3 the property MapAreas doesn't exits (or at least it doesn't appear to me)

So what i can do to build an structure like /Admin/Users/EditUser?id=2 for example?

If i need to create a route rule, can you write me an example of how to do it.


Solution

  • Routing rules are definitely the way to go. To make a structure like you mentioned, write the route rule like this:

    routes.MapRoute(
        "user_routing",
        "Admin/{controller}/{action}?id={id}",
        new { }
    );
    

    Then create a controller named UsersController, and an action with id as a parameter:

    public ActionResult EditUser(string id) {
        ...
    }