3

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

2 Answers 2

4

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)
{
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Darin, that was exactly what i was looking for. Cheers ;)
-2

You are using a wrong URL. Try this:

http://app.domain/Users.aspx/Details/114142?page=5

2 Comments

Except that he doesn't want /Users.aspx/Details/114142?page=5. He wants /Users.aspx/Details/114142/5.
Darin is right. In fact, i was using the url with parameters. Now, i want to stop using the page=5 to pass the 5 as page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.