ASP。NET MVC: url路由和querystring

时间:2021-11-13 04:15:26

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 ?

我添加了一个排序函数(按日期、作者等)。最好的处理方法是什么?/评论/搜索/ 3 /排序/作者/评论/搜索/ 3 ?作者= ?

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

如何在MVC中自动将querystring sort=author作为参数处理?

Thanks

谢谢

3 个解决方案

#1


58  

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.

我更喜欢:/评论/搜索/ 3 ? =作者。querystring是传递编程参数的好地方,特别是如果参数(像这种情况下)对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将自动将querystring值连接到方法的参数。

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")

/评论/搜索/ 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

#2


19  

ASP.NET MVC will handle that automatically in the query string case. You just add a string sort parameter to your action.

ASP。NET MVC将在查询字符串的情况下自动处理它。您只需向操作添加一个字符串排序参数。

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.

哪个更好?就我个人而言,我使用路径来控制显示的内容和querystring来控制表示(如何显示、格式化、……)。因此,对于排序,我将使用querystring方法。但我认为这两种方法都没有技术上的缺陷。

#3


2  

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会读/评论/搜索/3/排序/作者。

#1


58  

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.

我更喜欢:/评论/搜索/ 3 ? =作者。querystring是传递编程参数的好地方,特别是如果参数(像这种情况下)对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将自动将querystring值连接到方法的参数。

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")

/评论/搜索/ 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

#2


19  

ASP.NET MVC will handle that automatically in the query string case. You just add a string sort parameter to your action.

ASP。NET MVC将在查询字符串的情况下自动处理它。您只需向操作添加一个字符串排序参数。

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.

哪个更好?就我个人而言,我使用路径来控制显示的内容和querystring来控制表示(如何显示、格式化、……)。因此,对于排序,我将使用querystring方法。但我认为这两种方法都没有技术上的缺陷。

#3


2  

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会读/评论/搜索/3/排序/作者。