HuwD
HuwD

Reputation: 1810

MVC4 MapRoute for CMS

Am building a CMS using MVC4. My experience with MVC is limited and trying to create a MapRoute that can handle the page structured created by the CMS. URLs for the pages would be along the lines of website.com/About

To handle this I have come up with the following

        routes.MapRoute(
            name: "Default",
            url: "{p}",
            defaults: new { controller = "Home", action = "Index", p = UrlParameter.Optional }
        );

This works fine for root level pages but if I want sub pages like website.com/About/OurTeam

I get a 404. Ideally what I would like is just be able pass either the whole url after the .com bit as a string and sort it out in the controller or to split the levels up as an array of parameters and pass that through as 'p'.

Hope that makes sense. Any ideas?

Upvotes: 1

Views: 206

Answers (1)

Henk Mollema
Henk Mollema

Reputation: 46651

You can use:

routes.MapRoute(
            name: "Default",
            url: "{*p}",
            defaults: new { controller = "Home", action = "Index", p = UrlParameter.Optional }
        );

The asterisk indicates that it's a catch-all route. Keep in mind that these routes are extremely greedy, make sure that this stays below any specific routes.

You could also add a route constraint to this route which can determine whether the page exists in the database or something. See this post for more info.

Upvotes: 2

Related Questions