Wednesday, April 20, 2011

ASP MVC Url Format

Say you have a page that provides a list of songs depending on some identifier you have specified, such as a category it belongs to. You may have your URL formatted like so:

http://mysite.com/songs/view/1

Now lets say you wanted to add paging, what is the best way to structure that URL and how does the action method work? I am assuming something like this is incorrect

http://mysite.com/songs/view/1/page/2

Thanks

From stackoverflow
  • I think that that would be fine, or some variations:

    • /songs/view/1/page2
    • /songs/view/1?page=2
    • /songs/view/1/2

    or you could keep it in a cookie or something, and just not have to deal with it in the URL at all.

  • If you have your url as http://mysite.com/songs/view/1/page/2 you would always require that the word page to be in the url.

    Now if it was http://mysite.com/songs/view/1/2 where 2 being the page number and nullable you could still call http://mysite.com/songs/view/1 which would be your first page.

    The routing engine is smart enough to append the page number when it has a value.

    public ActionResult View(int id, int? page)
    {
    
    }
    
    MapRoute("songs.view", "songs/view/{id}/{page}", new { controller = "songs", action = "view", id = "", page = "1" });
    

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.