ASP.NET MVC:URL路由VS查询字符串 [英] ASP.NET MVC: url routing vs querystring

查看:103
本文介绍了ASP.NET MVC:URL路由VS查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢排到页/评论/搜索/ 3 其中i搜索和显示线程的所有意见3。

I have a page routed like /Comments/Search/3 where i search and display all the comments of the thread "3".

我添加一个排序功能(按日期,作者等)。什么是处理它的最佳方法? /评论/搜索/ 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 ?

我如何自动处理查询字符串排序= author作为MVC参数?

How do I automatically handle the querystring sort=author as a parameter in MVC?

感谢

推荐答案

我preFER:?/评论/搜索/ 3 =排序作者。查询字符串是纲领性的参数传递,尤其是如果参数(如在这种情况下)不是为搜索引擎优化的目的,重要的好地方。如果参数有一些语义的搜索词,第一个URL会更好。

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将自动线了查询字符串值,以你的方法的参数。

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

使用下面的路线

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

/评论/搜索/ 3?排序=笔者将调用搜索(3,作者)

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

/评论/搜索/ 3将调用搜索(3,NULL)

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

请该ID是强制性所以这个URL将会失败:
/评论/搜索

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

这篇关于ASP.NET MVC:URL路由VS查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆