asp.net-mvc ASP.NET MVC:url 路由与查询字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/968904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC: url routing vs querystring
提问by pistacchio
I have a page routed like /Comments/Search/3where 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/Authoror /Comments/Search/3?sort=author?
我正在添加一个排序功能(按日期、作者等)。处理它的最佳方法是什么?/Comments/Search/3/Sort/Author或者/Comments/Search/3?sort=author?
How do I automatically handle the querystring sort=author as a parameter in MVC?
如何自动处理查询字符串 sort=author 作为 MVC 中的参数?
Thanks
谢谢
回答by Praveen Angyan
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.
我更喜欢:/Comments/Search/3?sort=author。查询字符串是传递编程参数的好地方,尤其是在参数(如本例中)对于 SEO 目的不重要的情况下。如果该参数作为搜索词具有某种语义意义,则第一个 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
回答by Mehrdad Afshari
ASP.NET MVC will handle that automatically in the query string case. You just add a string sortparameter to your action.
ASP.NET MVC 将在查询字符串情况下自动处理。您只需string sort为您的操作添加一个参数。
Which is better? Personally, I use the path to control the contentsbeing 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.
哪个更好?就个人而言,我使用路径来控制正在显示的内容和查询字符串来控制演示文稿(如何显示、格式化等)。因此,对于排序,我会使用 querystring 方法。但我认为这两种方法都没有技术劣势。
回答by Fenton
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

