Cameron
Cameron

Reputation: 28853

Quick ASP.NET routing question

How can I turn this /Home/About/ into just /About/ using the rules in the Global.aspx file?

Upvotes: 4

Views: 90

Answers (6)

ARM
ARM

Reputation: 2385

I used this:

routes.MapRoute(
    "HomeActions",
    "{action}",
    new
    {
        controller      = "Home",
        action          = "Index"  // I technically don't think this is required.
    },
    new  // The second object are route constraints
    {
        action          = "Index|FAQ|ContactUs|Examples|Materials|Members" // <-- various actions on my home controller.  Any regex works here.
    });

Upvotes: 0

B Z
B Z

Reputation: 9463

Create a new route before the default route like this:

routes.MapRoute("about", "About", new { controller = "YourController", action = "About" });

Upvotes: 1

Kelsey
Kelsey

Reputation: 47766

Add a route:

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

Since it is hardcoded, you want to make sure it is before any routes that have placeholders for the various params.

Upvotes: 1

Scott Lance
Scott Lance

Reputation: 2249

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

Just make sure it is place before the default route handler.

Upvotes: 3

Serge Wautier
Serge Wautier

Reputation: 21898

   public static void RegisterRoutes(RouteCollection routes)
    {
      routes.MapRoute(
          "About", // Route name
          "About", // URL
          new { controller = "Home", action = "About" });

      ...

    }

@Scott: Thanks. Fixed it.

Upvotes: 6

Related Questions