The Muffin Man
The Muffin Man

Reputation: 20004

MVC 4 custom routes

I have the following routes defined:

routes.MapRoute(
            "Content Pages",
            "{action}",
            new { controller = "Pages", action = "Index" });

routes.MapRoute(
            "Default",
            "{controller}/{action}/{id}/{title}",
             new { controller = "Home", action = "Index", 
                   id = UrlParameter.Optional, 
                   title = UrlParameter.Optional },
             new string[] { "MyCompany.Web.Controllers" });

I have a controller named Pages that has some actions like "About", "FAQ" etc that I would like to access like this: mywebsite.com/About

This currently works, but now all of my other controllers end up specifying their default action in the url. My action link for Books renders as mywebsite.com/Books/Index.

How can I modify my routes so that I can achieve this?

Upvotes: 1

Views: 477

Answers (1)

dove
dove

Reputation: 20674

I would have a more generic route for all your other controllers like this (but put it after the Page routes below)

routes.MapRoute(
            "Content Pages",
            "{controller}",
            new { Home= "Home", action = "Index" });

and change your Pages one to be more specific for those two actions (assuming you have not got an About or Faq controller).

routes.MapRoute("About Page", "about", new { controller = "Pages", action = "About" });
routes.MapRoute("FAQ Page", "faq", new { controller = "Pages", action = "FAQ" });

Upvotes: 1

Related Questions