2

I have seen a few similar posts, but none are quite the same because in my question, I want to remove the HOME controller form the URL but the controller to remain as the default (so when my site loads from site.com/ the Index in my Home controller is fired)!!

My MVC4 website has several controllers, such as

Home
Products
Details
AndAnother
YetAnother

The Home controller is my default controller, and is where my home page lives, about page, contact us etc

I want to remove the HOME from the URL but this is still my default controller. All other controllers should remain with the controller name.

The following doesn't work, please note the default controller "WhatControllerGoesHere"

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

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

If I make both default controller HOME, I can't navigate to any other controller

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

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

So, the URLs I'm trying to achieve would be (where about and contact us are views in my Home)

site.com/                 //note, no Home/
site.com/about            //note, no Home/  
site.com/contact          //note, no Home/
site.com/products/  
site.com/products/other  
site.com/details/  
site.com/details/other  
site.com/details/AndAnother  

I know I can create new routers for each controller but I'd rather avoid that if possible.

MyDaftQuestions
  • 4,487
  • 17
  • 63
  • 120
  • Are you actually wanting `site.com/index` rather than just `site.com/` for the `Index()` method of the `HomeController`? –  Apr 21 '15 at 01:03
  • @StephenMuecke, no, more about showing all the controllers except the HOME controller. My post has been updated – MyDaftQuestions Apr 22 '15 at 06:38

1 Answers1

3

All the url's you have shown except site.com/about and site.com/contact will be handled correctly by the default route. To remove Home/ prefix from the about and contact url, you need to add 2 specific routes before the default (assumes you don't need an id parameter)

routes.MapRoute(
    name: "About",
    url: "About",
    defaults: new { controller = "Home", action = "About" }
);
routes.MapRoute(
    name: "Contact",
    url: "Contact",
    defaults: new { controller = "Home", action = "Contact" }
);
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
  • 1
    Just an update: with MVC 5 you can also use `Attribute Routing`. See https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/ – Gianpiero Aug 30 '17 at 16:11