Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC C# Routing - Passing a null integer

I'm working with MVC 3 in a web app and i'm facing a problem in routing.

I'm defining my router handler like this:

           routes.MapRoute(
           "Users", 
           "{controller}.aspx/{action}/{id}/{page}", // URL with parameters
           new { controller = "Users", action = "Details", id = UrlParameter.Optional, page = UrlParameter.Optional } // Parameter defaults
       );

The url is: http://app.domain/Users.aspx/Details/114142/5 (example)

I'm sucefully getting the id of the user, but i can't get the page number.

The controller of users is initialized like this:

           public ActionResult Details(long id, int? page)

The page is always returning null (i need the page as a null integer).

And i defining the route wrong?

Thanks

like image 370
Gui Avatar asked Mar 21 '26 19:03

Gui


1 Answers

id cannot be optional if page is optional. Only the last parameter of a route definition can be optional.

So :

routes.MapRoute(
    "Users", 
    {controller}.aspx/{action}/{id}/{page}",
    new { 
        controller = "Users",  
        action = "Details", 
        page = UrlParameter.Optional 
    }
);

and then: /Users.aspx/Details/114142/5 will successfully map to

public ActionResult Details(long id, int? page)
{
    ...
}
like image 115
Darin Dimitrov Avatar answered Mar 25 '26 11:03

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!