8

We're considering moving forward with a ASP.NET MVC project and the subject of routing versus parameters came up.

Seeing as how you can easily set up either or a combination of both in ASP.NET MVC, are there any considerations that I should be aware of when using one or the other?

mwright
  • 4,099
  • 7
  • 30
  • 40

2 Answers2

6

I would recommend keeping your URL's as clean as possible and to try and use routes whenever possible. You should try and make RESTful URI's that will convey information to the user. For example:

www.yourdomain.com/Products/Sports/Clothing

is a lot cleaner than

www.yourdomain.com/Products?Department=Sports&SubDepartment=Clothing 

If you use a ton of query strings then it won't be a clean URI and less information is conveyed to the user.

With that said, our team does use query strings for ajax type requests using jquery. This is because these URI's are in our markup and won't be seen in the browser window. This has helped keep our global.asax a little smaller since it won't get polluted with a ton of routes.

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
amurra
  • 15,221
  • 4
  • 70
  • 87
5

At my project we only use querystrings for optional values. Thats mostly filtering, sorting and paging lists. Optional values are difficult to handle in a route.

Its much harder to maintain a querystring in the URL. They are not rendered when using ActionLink and other routing aware helpers.

Mathias F
  • 15,906
  • 22
  • 89
  • 159
  • I can understand "Optional values are difficult to handle in a route". Can you be more clear about that sentence. – Anirudha Gupta Jan 30 '15 at 11:20
  • 2
    The url will have the optional value in the path. Lets say you have optional values "OrderBy" and "FilterBy". What would that look like in the path? Maybe something like /mycontroller/myaction/orderbyprice/filterbyisavailable. If OrderBy is not present your path would become /mycontroller/myaction/{a value mapped to null}/filterbyisavailable. Its ok if you have optional values at the end of the route. – Mathias F Jan 30 '15 at 13:38