Addendum: param = to mvc routes

Some MVC sites have query parameters added to the Url route (from which I noticed StackOverflow), for example:

stack overflow page = 9802 & sort = newest & pagesize = 15

What are the benefits of having parameters as more common querystring parameters than / param / values ​​/?

Also, how are these parameters added to the routes that have been configured? I am familiar with configuring mvc routes with parameters like "users / details / {id}" etc. But I don’t know how to configure routes for use with 1 or more parameters according to the url example above?

+7
source share
2 answers

Query string parameters are useful when you have several optional parameters, and do not want to include default values ​​for parameters not specified just to satisfy the path.

And you don’t have to do anything special to include these parameters in the display URL.

Take the following route, for example:

routes.MapRoute ( "QuestionsTagged", "questions/tagged/{tag}", new { controller = "Questions", action = "Tagged" } ); 

If you pass a link to this route using:

 Url.RouteUrl ( "QuestionsTagged", new { tag = "java", page = 9802, sort = "newest", pagesize = 15 } ) 

... then the routing mechanism is smart enough to see that the route contains a parameter named tag and that the object of the transferred route objects also has something called tag , so it uses this value in the route.

Any provided route values ​​that do not have corresponding parameters in the route ( page , sort and pagesize in this case) are inserted as query string parameters. So calling Url.RouteUrl above would return /questions/tagged/java?page=9802&sort=newest&pagesize=15 .

And your action method can explicitly list these parameters in its signature (improves readability and maintainability), or you can access them through Request.QueryString .

 public class QuestionsController : Controller { // I can explicitly list the parameters in my signature and let routing do // its magic, like this... public ViewResult Tagged(string tag, int? page, int? pagesize) { // ...or I can grab parameters like this: string sort = Request.QueryString["sort"]; return View(); } } 

Please note that the parameters of the action method must not match the parameters specified in the route. (In the route, I specified only tag , but in the list of signature of the action method tag , page and pagesize .) However, any parameter of the action method, which is also not a parameter of the route, must be a reference or null type.

+4
source

I usually saw paging and filtering data being passed as query parameters, as they provide information to the user in a URI. It is also normally harmless if the user modifies this data, as he simply filters the data that you see on the page. Any sensitive data is usually published so that it is not so easy to see or change, but I would say that your URI should clean and use quesrystrings as little as possible.

You do not need to do anything special when specifying routes in order to be able to handle quesrystrings. They will simply be additional data that is passed to your action. However, in your action, you will need to do some data processing work. Using the above, you will need to specify the query names as parameter names, and then any data type that you expect.

 public ActionResult Index (int page, string sort, int pagesize) 

In this example, the page will be 9802, the sort will be β€œnewest”, and the page size will be 15.

+1
source

All Articles