5

I'm using ASP.NET MVC 4, .NET 4.5. Any way, besides creating individual controllers for each "action", to have "controller-less" urls?

What I mean is, have a Home controller filled with actions. Urls such as:

  • site.com/Home/About
  • site.com/Home/Contact

to become

  • site.com/About
  • site.com/Contact

but still use the Home controller.

1 Answer 1

7

You can define routes that don't contain the controller name like this:

routes.MapRoute(
    "About",                                       // Route name
    "About/",                                      // URL with parameters
    new { controller = "Home", action = "About" }  // Parameter defaults
);

routes.MapRoute(
    "Contact",                                       // Route name
    "Contact/",                                      // URL with parameters
    new { controller = "Home", action = "Contact" }  // Parameter defaults
);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this worked perfectly: routes.MapRoute( name: "NoController", url: "{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.