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 Commented 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
);
Sign up to request clarification or add additional context in comments.

6 Comments

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.
Can you paste that route here?
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.
I recommend using Phil Haack's route debugger: haacked.com/archive/2008/03/13/url-routing-debugger.aspx
Good point; I updated the question to include all my routes. I'll check out the route debugger that was posted. Thanks!
|
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");
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.