在 MVC 中,将多个值作为查询字符串传递是好还是坏做法

发布于 2025-01-03 01:15:06 字数 331 浏览 1 评论 0原文

在我的页面中,我必须将多个值传递给控制器​​。

我的网址看起来像: http://blah/Search/Page/?type=new&keywords =blahblah&sortType=Date

在 MVC 中将多个值作为查询字符串传递是一个好的做法吗?或者我们可以通过引入自定义路由来使用斜杠分隔 URL?

注意:让我们考虑查询字符串中的所有值,都不是安全/敏感数据。

In my page I ve to pass multiple values to controller.

My URL looks something like :
http://blah/Search/Page/?type=new&keywords=blahblah&sortType=Date

Is Passing multiple values as query string good practice in MVC ? or We can have slash separated URL, by using introducing custom routing?

NB: Lets consider all the values in my query string, are not secure / sensitive data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

强者自强 2025-01-10 01:15:06

只要不是敏感数据,我就不认为这是一种不好的做法。这仅取决于您是否想在 global.asax 中编写自定义路由来处理它。自定义路由确保提供更清晰的 url。此外,对于更精明的用户来说,如果他们理解您网站上的概念,那就更直观。

因此考虑一下:

http://baseballcards/topps/1980      // search for baseball cards made by topps in the 1980s
http://recipes/deserts/pies         // search for a desert recipe that are pies  
http://code/csharpe/linq             // search for csharp code that has linq examples

在这些示例中,我们几乎可以像句子一样读取 url,使其更加直观,从而使用户能够即插即用。它清楚地表示查询,就像面包屑一样,准确地指示上下文是什么。我个人喜欢这个。但无论哪种方式都是一个好方法。

使用更多参数扩展:

routes.MapRoute(
            "SearchRecipes",                                              
            "Search/Recipes/{category}/{type}",                           
            new { controller = "Search", action = "Recipes", category = "all" , type = ""}  
 );

一些示例:

Search/Recipes/Deserts/Pie
Search/Recipes/Dinner/Beef
Search/Recipes/Lunch/Salads

I wouldn't consider it a bad practice, as long as it's not senstive data. It just depends if you want to write a custom route in your global.asax to handle it or not. The custom routes provide a cleaner url forsure. Also, for more savy users if they understand the concept on your site, it's more intuitive.

So consider this:

http://baseballcards/topps/1980      // search for baseball cards made by topps in the 1980s
http://recipes/deserts/pies         // search for a desert recipe that are pies  
http://code/csharpe/linq             // search for csharp code that has linq examples

In these examples we can almost read the url like a sentence, making it more intuitive, giving a user the ability to plug and play. It clearly denotes the query almost like a breadcrumb, indicating exactly what the context will be. I personally like this. But either way is a good approach.

To extend with more parameters:

routes.MapRoute(
            "SearchRecipes",                                              
            "Search/Recipes/{category}/{type}",                           
            new { controller = "Search", action = "Recipes", category = "all" , type = ""}  
 );

Some examples:

Search/Recipes/Deserts/Pie
Search/Recipes/Dinner/Beef
Search/Recipes/Lunch/Salads
不忘初心 2025-01-10 01:15:06

请选择稍后(路由值中的查询字符串)

  1. 如果您担心标头长度,
  2. 。(默认情况下,获取参数是标头的一部分,并且 Web 服务器默认在 IIS7 中接受 1024 字节标头长度)。隐藏代码的逻辑实现。
  3. 网址看起来不错,也更容易记住。

否则,这两种方法效果相同。

Select later (query string in route values) in case,

  1. If you are concerned about header length.( By default get parameters are part of headers, and web server accept 1024 byte header length by default in IIS7).
  2. Hide logical implementation of your code.
  3. Url looks good and easier to remember.

Otherwise Both the approaches work equally.

我喜欢麦丽素 2025-01-10 01:15:06

我认为在查询字符串中传递搜索参数是您应该采取的方式,特别是如果所有参数都是可选的。它还使您能够轻松使用普通的 method="get" 表单。

我不认为“安全/个人数据”与此有任何关系,因为查询字符串是 URL 的一部分,就像路径一样。

I think passing search parameters in the query string is the way you should go, especially if all your parameters are optional. It also enables you to use normal method="get" forms without hassle.

I don't think "security/personal data" has anything to do with this since the query string is a part of the URL just like the path is.

╰沐子 2025-01-10 01:15:06

IMO 我认为这绝对没问题。我认为当路径表示函数并且查询字符串参数表示过滤器时(例如在搜索中),实际上最好在 MVC 中使用查询字符串。

但是,我不会将查询字符串参数用于表示有关检索到的内容的信息的数据。因此,如果返回一年和月份的多页文章,我会在路径中包含文章的年份和月份,但不包含页码。

IMO I think this is absolutely fine. I think it is actually preferable to use querystrings in MVC when the path represents the function and the querystring parameters represent the filters, e.g. as in a search.

However, I wouldn't use querystring parameters for data that represent information about the content retrieved. So I would include the year and month of an article in the path but not the page number if returning more than one page of articles for the year and month.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文