Libin TK
Libin TK

Reputation: 1487

MVC4 RouteConfig params return null

My Booking Controller have the following code

public ActionResult Index(string id, string name)
{
    return View();
}

and my routeConfig have the below route mappings

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
        routes.MapRoute(
            name: "Search",
            url: "{controller}/{location}/{checkIn}/{checkOut}/{no}",
            defaults: new { controller = "Search", action = "Index", location = UrlParameter.Optional, checkIn = UrlParameter.Optional, checkOut = UrlParameter.Optional, no = UrlParameter.Optional }
        );
        routes.MapRoute(
            name: "booking",
            url: "{controller}/{action}/{id}/{name}",
            defaults: new { controller = "Booking", action = "Index", id = UrlParameter.Optional, name=UrlParameter.Optional }
        );}

but when I access the page http://localhost:59041/booking/index/1/libin both params returns null.

Upvotes: 2

Views: 2530

Answers (1)

user1968030
user1968030

Reputation:

see this book

As your application becomes more complex you are likely going to register multiple routes. When you do this its important that you consider the order that that you register them. When the routing engine attempts to locate a matching route, it simply enumerates the collection of routes and it stops enumerating as soon as it find a match.

Add a comment This can cause plenty of problems if you’re not expecting it. Let’s look at an examples where this can be a problem:

routes.MapRoute(
    >     "generic", // Route name
    >     "{site}", // URL with parameters
    >     new { controller = "SiteBuilder", action = "Index" } // Parameter defaults );
    > 
    > routes.MapRoute(
    >     "admin", // Route name
    >     "Admin", // URL with parameters
    >     new { controller = "Admin", action = "Index" } // Parameter defaults );

The snippet above registers two routes. The first route

contains a single placeholder segment and sets the default value of the controller parameter to SiteBuilder. The second route contains a single constant segment and sets the default value of the controller parameter to Admin.

Both of these routes are completely valid, but the order in which they are mapped may cause unexpected problems because the first route matches just about any value entered, which means that it will be the first to match

http://example.com/Admin and since the routing engine stops after finding the first match, the second route would never get used.

So, be sure to keep this scenario in mind and consider the order in which you define custom routes.

You should write booking routes at first

  routes.MapRoute(
            name: "booking",
            url: "{controller}/{action}/{id}/{name}",
            defaults: new { controller = "Booking", action = "Index", id = UrlParameter.Optional, name=UrlParameter.Optional }
        );}
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
        routes.MapRoute(
            name: "Search",
            url: "{controller}/{location}/{checkIn}/{checkOut}/{no}",
            defaults: new { controller = "Search", action = "Index", location = UrlParameter.Optional, checkIn = UrlParameter.Optional, checkOut = UrlParameter.Optional, no = UrlParameter.Optional }
        );

Upvotes: 2

Related Questions