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
);
}
In the custom routing mode MVC sites respond to incoming requests using standard ASP.NET routing. Page URLs are determined by the routes that you register into your MVC application's routing table.
The ASP.NET Routing module is responsible for mapping incoming browser requests to particular MVC controller actions. By the end of this tutorial, you will understand how the standard route table maps requests to controller actions.
A route is a URL pattern. Routing is a pattern matching process that monitors the requests and determines what to do with each request. In other words we can say Routing is a mechanism for mapping requests within our MVC application. The Routing mechanism passes the request to the handler.
Every ASP.NET MVC application must configure (register) at least one route in the RouteConfig class and by default, the ASP.NET MVC Framework provides one default route. But you can configure as many routes as you want.
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
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With