简体   繁体   English

ASP.NET MVC:url routing vs querystring

[英]ASP.NET MVC: url routing vs querystring

I have a page routed like /Comments/Search/3 where i search and display all the comments of the thread "3". 我有一个页面路由像/Comments/Search/3 ,我搜索并显示线程“3”的所有注释。

I'm adding a sort function (by date, author etc). 我正在添加一个排序功能(按日期,作者等)。 What is the best way to handle it? 处理它的最佳方法是什么? /Comments/Search/3/Sort/Author or /Comments/Search/3?sort=author ? /Comments/Search/3/Sort/Author or /Comments/Search/3?sort=author

How do I automatically handle the querystring sort=author as a parameter in MVC? 如何在MVC中自动处理查询字符串sort = author作为参数?

Thanks 谢谢

I prefer: /Comments/Search/3?sort=author. 我更喜欢:/ 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. 查询字符串是传递编程参数的好地方,特别是如果参数(如本例中)对于SEO目的不重要。 If the parameter had some semantic meaning as a search term, the first URL would be better. 如果参数具有某种语义含义作为搜索词,则第一个URL会更好。

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. ASP.NET MVC将自动将查询字符串值连接到方法的参数。

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?sort = author将调用Search(3,“author”)

/Comments/Search/3 will call Search(3, null) / Comments / Search / 3将调用Search(3,null)

Keep in mind that id is mandatory so this url will fail: /Comments/Search 请记住,id是必需的,因此此URL将失败:/ Comments / Search

ASP.NET MVC will handle that automatically in the query string case. ASP.NET MVC将在查询字符串的情况下自动处理它。 You just add a string sort parameter to your action. 您只需在操作中添加string sort参数即可。

Which is better? 哪个更好? Personally, I use the path to control the contents being displayed and querystring to control the presentation (how it's displayed, formatted, ...). 就个人而言,我使用路径来控制正在显示的内容和查询字符串来控制演示文稿 (如何显示,格式化......)。 So, for sorting, I'd go with the querystring method. 所以,为了排序,我会使用查询字符串方法。 But I don't think there's a technical disadvantage in either approach. 但我不认为这两种方法都存在技术劣势。

Your best bet is to add a routing rule to handle it. 最好的办法是添加一个路由规则来处理它。 There's a handy article on it here: 这里有一篇方便的文章:

http://aspalliance.com/1525_ASPNET_MVC_Framework_Part_2_URL_Routing.2 http://aspalliance.com/1525_ASPNET_MVC_Framework_Part_2_URL_Routing.2

Then your URL would read /Comments/Search/3/Sort/Author 然后你的URL会读/ Comments / Search / 3 / Sort / Author

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM