Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 4 Remove "home" from base route

Basically I want to make it so that: http://website.com/Home/About

Shows up as: http://website.com/About

The "home" controller showing up in the url would make the url longer for the user to read.

I tried to do the following:

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

Could someone help me out please?

like image 366
BRogers Avatar asked Oct 10 '12 21:10

BRogers


2 Answers

Try this. It also makes your URLs lowercase.

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.LowercaseUrls = true;

        routes.MapMvcAttributeRoutes();

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

And in your Home controller:

    [Route("About")]
    public ActionResult About()
    {
        return View();
    }
like image 31
Eric Murr Avatar answered Nov 01 '22 01:11

Eric Murr


Try something like this:

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );
like image 171
webdeveloper Avatar answered Nov 01 '22 02:11

webdeveloper