Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Routing - "Blank" Route

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
        );
    }
like image 759
blaster Avatar asked Nov 03 '10 14:11

blaster


People also ask

What is custom route in MVC?

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.

What is ASP NET MVC routing?

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.

How does route work in MVC?

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.

How many routes are there in MVC?

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.


1 Answers

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
);
like image 58
dotariel Avatar answered Sep 21 '22 13:09

dotariel