37

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?

Red
  • 818
  • 2
  • 14
  • 26
BRogers
  • 3,534
  • 4
  • 23
  • 32

3 Answers3

58

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
    );
webdeveloper
  • 17,174
  • 3
  • 48
  • 47
  • That worked perfectly! Thank you.... the order matters here... I did that exactly earlier, however... it was under the Default route which made it not work. Your awesome, thanks! – BRogers Oct 10 '12 at 21:27
  • Where to add this one, which file? – Nimitz E. Apr 18 '15 at 00:51
  • If your homepage differs from the route you are trying to manipulate change `{action}` to `action`. I was trying to remove controller name for the AboutUs page but doing this just made the AboutUs page the "home page" – CSharper Sep 11 '15 at 17:06
  • It's not apporpriate soluition because for (as example) `Index` page of `Manage` contoroller not working with `domain.com/manage` – Amir Astaneh Nov 04 '16 at 04:54
9

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();
    }
Eric Murr
  • 91
  • 1
  • 2
  • Remember that you can specify the links in lowercase and it shows as lowercase in the address bar too. It is not case-sensitive. – Exel Gamboa Nov 09 '15 at 20:27
2

Step 1: Create the route constraint.

public class RootRouteConstraint<T> : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            var rootMethodNames = typeof(T).GetMethods().Select(x => x.Name.ToLower());
            return rootMethodNames.Contains(values["action"].ToString().ToLower());
        }
    }

Step 2:
Add a new route mapping above your default mapping that uses the route constraint that we just created. The generic parameter should be the controller class you plan to use as your “Root” controller.

routes.MapRoute(
                "Root",
                "{action}",
                new {controller = "Home", action = "Index", id = UrlParameter.Optional},
                new {isMethodInHomeController = new RootRouteConstraint<HomeController>()});




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

Now you should be able to access your home controller methods like so: example.com/about, example.com/contact

This will only affects HomeController. Alll other Controllers will have the default routing functionality.

Rahul
  • 2,431
  • 3
  • 35
  • 77
  • 6
    Do not copy / paste your answer to multiple questions. If you are able to copy and paste, the question is likely a duplicate and should be closed as such. – Andy Jan 15 '16 at 19:04