在 MVC 中,将多个值作为查询字符串传递是好还是坏做法
在我的页面中,我必须将多个值传递给控制器。
我的网址看起来像: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只要不是敏感数据,我就不认为这是一种不好的做法。这仅取决于您是否想在 global.asax 中编写自定义路由来处理它。自定义路由确保提供更清晰的 url。此外,对于更精明的用户来说,如果他们理解您网站上的概念,那就更直观。
因此考虑一下:
在这些示例中,我们几乎可以像句子一样读取 url,使其更加直观,从而使用户能够即插即用。它清楚地表示查询,就像面包屑一样,准确地指示上下文是什么。我个人喜欢这个。但无论哪种方式都是一个好方法。
使用更多参数扩展:
一些示例:
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:
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:
Some examples:
请选择稍后(路由值中的查询字符串)
否则,这两种方法效果相同。
Select later (query string in route values) in case,
Otherwise Both the approaches work equally.
我认为在查询字符串中传递搜索参数是您应该采取的方式,特别是如果所有参数都是可选的。它还使您能够轻松使用普通的
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.
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.