8

Can I set up a route that will get mapped from a root-level URL like this?

http://localhost:49658/

I'm using the VS2010 built-in web server.

Attempting to set up a route with a blank or a single-slash URL string doesn't work:

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

It results in the error "The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.". Thanks in advance! My entire route definition is here:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "EditingTitles", // Route name
            "{controller}/{action}/{startingLetter}", // URL with parameters
            new { controller = "Admin", action = "Index", startingLetter = UrlParameter.Optional } // Parameter defaults
        );

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    }
1
  • Show us your other routes, I had a problem similar to above (and your notes on XSaint32's answer), my default route was resulting in a 404 unless i specified a controller/action and it was due to a bad named route eslewhere in my routes
    – Jimmy
    Nov 3, 2010 at 14:42

2 Answers 2

10

What are you trying to achieve here... a URL that looks like this? http://www.acme.com/ ? Because if you are, the default route will achieve that when none of the parameters are specified.

// Default Route:
routes.MapRoute(
   "Default", // Route name
   "{controller}/{action}/{id}", // URL with parameters
   new { controller = "Home", action = "Index", id = String.Empty } // Parameter defaults
);
6
  • You're right, that URL is what I'm trying to achieve. I already have a route exactly like the one you specify . . . but I get an HTTP 404 ("the resource could not be found") error when I F5 my project and it hits the default URL, so it seemed like I had to somehow handle the "empty URL" case specifically.
    – blaster
    Nov 3, 2010 at 14:18
  • Can you paste that route here?
    – dotariel
    Nov 3, 2010 at 14:22
  • Sounds like another specified route is bad, I had a similar problem and my default route was being overwritten and not handling the root url. Please post all of your routes in the question.
    – Jimmy
    Nov 3, 2010 at 14:46
  • 1
    I recommend using Phil Haack's route debugger: haacked.com/archive/2008/03/13/url-routing-debugger.aspx
    – dotariel
    Nov 3, 2010 at 14:48
  • Good point; I updated the question to include all my routes. I'll check out the route debugger that was posted. Thanks!
    – blaster
    Nov 3, 2010 at 16:34
5

Using ASPNET MVC5: RouteConfig.cs file:

 public static void RegisterRoutes(RouteCollection routes)
 {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
        name: "Homepage",
        url: "",
        defaults: new { controller = "Content", action = "Index" }
    );
    routes.MapRoute(
        name: "foo",
        url: "bar",
        defaults: new { controller = "Content", action = "Index" }
    );
    routes.MapMvcAttributeRoutes();

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{title}",
        defaults: new { controller = "Content", action = "Details", title = UrlParameter.Optional }
    );
}

Plus:
If you wishes to redirect your homepage to another route automatically, like "http://www.yoursite.com/" to "http://www.yoursite.com/bar", just use the method RedirectToRoute():

public class ContentController : Controller
    {
        public ActionResult Index()
        {
            return RedirectToRoute("foo");
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.