ASP.NET MVC: url routing vs querystring

I prefer: /Comments/Search/3?sort=author. The querystring is a good place to pass in programmatic parameters, especially if the parameter (like in this case) is not important for SEO purposes. If the parameter had some semantic meaning as a search term, the first URL would be better.

In a controller method you can use something like this:

public ActionResult Search(int id, string sort)

ASP.NET MVC will automatically wire up querystring values to the parameters of your method.

Use the following route

routes.MapRoute(
                   "Default",                                              // Route name
                   "{controller}/{action}/{id}",                           // URL with parameters
                   new { controller = "Comments", action = "Search", id = "" }  // Parameter defaults
               );

/Comments/Search/3?sort=author will call Search(3, “author”)

/Comments/Search/3 will call Search(3, null)

Keep in mind that id is mandatory so this url will fail:
/Comments/Search

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)