maxp
maxp

Reputation: 25161

Hierarchies in MVC3

Im working my way though an ASP.NET MVC tutorial and couldnt find the answer im looking for.

I understand that each controller class in the 'Controller' root folder is mapped to a Url, so:

****Controller Folder****
    |- StoreController.cs

Maps to $url/Store

However, If I wish to creater a 'subfolder'

I.e. a Controller class located for $url/Store/Testing I cant seem to see how I go about it. I tried deriving a class from StoreController.cs, but that didnt work.

Upvotes: 0

Views: 165

Answers (2)

Yuriy Guts
Yuriy Guts

Reputation: 2220

URLs do not necessarily correspond to MVC application internal folder structure. You can use MVC routing tables to conceal the internal structure and redirect specific URLs to any controllers/actions you want. For example, you can create a TestingController.cs class in the Controllers folder and use this route in Global.asax:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Store-Testing", // Route name
            "Store/Testing/{action}/{id}", // URL with parameters
            new { controller = "Testing", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }

In this case, a request to http://[domain]/Store/Testing will be handled by TestingController.

Upvotes: 1

Christofer Eliasson
Christofer Eliasson

Reputation: 33865

That url would with the default route point to an action called Testing, within the Store controller.

You can however create your own custom routes in your global.asax file.

Upvotes: 0

Related Questions