将对象作为 url 参数传递 c# [英] Passing objects as url parameters c#

查看:113
本文介绍了将对象作为 url 参数传递 c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 Filters 对象以及其他东西作为查询参数传递到 url 中,例如:

<代码> {"clientId": 2,"日期": "2017-01-01",过滤器":{天":{星期一",星期二",星期三"},月":{一月",二月"}}

但我不知道这个例子中像 filters 这样的对象是如何通过查询字符串参数传入的.我知道你通常会有这样的东西:

https://localhost/path?clientId=2&date=2017-01-01&filters= ?????

谢谢!

解决方案

如果数据很复杂,最好将其 POST,而不是作为查询字符串参数发送.但无论如何,如果您想作为查询字符串发送,您可以执行以下操作:

  1. 将对象转换为字符串
  2. 编码字符串
  3. 附加为参数

对于以下对象(转换为字符串并删除空格):

<预><代码>{"clientId": 2,"日期": "2017-01-01",过滤器":{天":{星期一",星期二",星期三"},月":{一月",二月"}}

我创建了可安全通过网络发送的编码文本:

%7B%0A%22clientId%22%3A%202%2C%0A%22date%22%3A%20%222017-01-01%22%2C%0A%22filters%22%3A%20%7B%0A%22days%22%3A%20%7B%20%22monday%22%2C%20%22tuesday%22%2C%20%22wednesday%22%20%7D%2C%0A%22months%22%3A%20%7B%20%22january%22%2C%20%22february%22%20%7D%0A%7D

就您而言,它将是:

https://localhost/path?clientId=2&date=2017-01-01&filters=%7B%0A%22clientId%22%3A%202%2C%0A%22date%22%3A%20%222017-01-01%22%2C%0A%22filters%22%3A%20%7B%0A%22days%22%3A%20%7B%20%22monday%22%2C%20%22tuesday%22%2C%20%22wednesday%22%20%7D%2C%0A%22months%22%3A%20%7B%20%22january%22%2C%20%22february%22%20%7D%0A%7D

您可以使用 meyerweb.com 来测试编码/解码,但在 C# 中您可以研究 HttpUtility.UrlEncode() 方法也可以在您的情况下使用.

I want to pass a Filters object as well as other things as query parameters into a url, for example something like:

   { 
       "clientId": 2, 
       "date": "2017-01-01",
       "filters": {
            "days": { "monday", "tuesday", "wednesday" },
            "months": { "january", "february" }
   }

But I don't know how an object like filters in this example could get passed in by a query string parameter. I know you would normally have something that looks like:

https://localhost/path?clientId=2&date=2017-01-01&filters= ?????

Thanks!

解决方案

Maybe is better to POST your data if they are complex instead sending as query string parameter. But anyway, if you want to send as query string you can do following:

  1. Convert object into string
  2. Encode string
  3. Append as parameter

For following object (converted into string and removed spaces):

{
   "clientId": 2,
   "date": "2017-01-01",
   "filters": {
   "days": { "monday", "tuesday", "wednesday" },
   "months": { "january", "february" }
}

I created encoded text which is safe to sending over network:

%7B%0A%22clientId%22%3A%202%2C%0A%22date%22%3A%20%222017-01-01%22%2C%0A%22filters%22%3A%20%7B%0A%22days%22%3A%20%7B%20%22monday%22%2C%20%22tuesday%22%2C%20%22wednesday%22%20%7D%2C%0A%22months%22%3A%20%7B%20%22january%22%2C%20%22february%22%20%7D%0A%7D

In your case it will be:

https://localhost/path?clientId=2&date=2017-01-01&filters=%7B%0A%22clientId%22%3A%202%2C%0A%22date%22%3A%20%222017-01-01%22%2C%0A%22filters%22%3A%20%7B%0A%22days%22%3A%20%7B%20%22monday%22%2C%20%22tuesday%22%2C%20%22wednesday%22%20%7D%2C%0A%22months%22%3A%20%7B%20%22january%22%2C%20%22february%22%20%7D%0A%7D

You can use meyerweb.com to test encoding/decoding but in C# you can research HttpUtility.UrlEncode() method which can be used in your situation too.

这篇关于将对象作为 url 参数传递 c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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