pis*_*hio 42 asp.net-mvc routing query-string
我有一个页面路由像/Comments/Search/3我搜索和显示线程"3"的所有注释.
我正在添加一个排序功能(按日期,作者等).处理它的最佳方法是什么?/Comments/Search/3/Sort/Author还是/Comments/Search/3?sort=author?
如何在MVC中自动处理查询字符串sort = author作为参数?
谢谢
Pra*_*yan 59
我更喜欢:/ Comments/Search/3?sort = author.查询字符串是传递编程参数的好地方,特别是如果参数(如本例中)对于SEO目的不重要.如果参数具有某种语义含义作为搜索词,则第一个URL会更好.
在控制器方法中,您可以使用以下内容:
public ActionResult Search(int id, string sort)
Run Code Online (Sandbox Code Playgroud)
ASP.NET MVC将自动将查询字符串值连接到方法的参数.
使用以下路线
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Comments", action = "Search", id = "" } // Parameter defaults
);
Run Code Online (Sandbox Code Playgroud)
/ Comments/Search/3?sort = author将调用Search(3,"author")
/ Comments/Search/3将调用Search(3,null)
请记住,id是必需的,因此此URL将失败:/ Comments/Search
Meh*_*ari 20
ASP.NET MVC将在查询字符串的情况下自动处理它.您只需string sort在操作中添加参数即可.
哪个更好?就个人而言,我使用路径来控制正在显示的内容和查询字符串来控制演示文稿(如何显示,格式化......).所以,为了排序,我会使用查询字符串方法.但我不认为这两种方法都存在技术劣势.
| 归档时间: |
|
| 查看次数: |
41806 次 |
| 最近记录: |