ASP.NET MVC Routing - Empty Route

Is it possible to configure a route to be displayed from a root level URL like this?

http: // localhost: 49658 /

I am using the built-in web server VS2010.

Trying to configure a route with a blank or single-slash URL string does not work:

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

This results in the error "The route URL cannot begin with the character" / "or" ~ ", and it cannot contain the character"? "the character.". Thanks in advance! My definition of the entire route 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 ); } 
+6
asp.net-mvc routing
source share
2 answers

What are you trying to achieve here ... URL that looks like this? http://www.acme.com/ ? Because, if you are, the default route will achieve this when none of the options 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 ); 
+8
source share

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 } ); } 

A plus:
If you want to automatically redirect your home page to another route, for example, " http://www.yoursite.com/ " to " http://www.yoursite.com/bar ", simply use the RedirectToRoute () method:

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

All Articles