简书链接:aspnetcoreapi同时getpostjson参数的传递研究
文章字数:381,阅读全文大约需要1分钟
为了简便参数,简单类型没必要用到get里面了,除非是大量数据

经过研究发现,下面代码实现了get 或者 post提交的普通get参数aa的同时 传递json的方法,

dynamic object 都会转换为JsonElement 也可以直接用JsonElement

在谷歌浏览器显示payload,这到底是什么鬼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

[HttpPost("getAndPost")]
//[HttpPost("ng={Ngtotal}&id={id}&username={username}")]
public String getAndPost(String aa, JsonElement param)

{


try
{
JObject obj = JObject.Parse(param + "");
return "data::" + param.GetType() + ",getParam:" + aa+",parse Success!";

}
catch (Exception e)
{
return "data::" + param.GetType() + ",getParam:" + aa+",parse fail!"+e.ToString();

}


}
[HttpGet("getAndPost1")]
//[HttpPost("ng={Ngtotal}&id={id}&username={username}")]
public String getAndPost1(String aa, JsonElement param)

{


try
{
JObject obj = JObject.Parse(param + "");
return "data::" + param.GetType() + ",getParam:" + aa+",parse Success!";

}
catch (Exception e)
{
return "data::" + param.GetType() + ",getParam:" + aa+",parse fail!"+e.ToString();

}

}

实际上 就是已经弄好了 get参数 +json 提交就ok了。

image.png
在浏览器看不到请求头的表达,只能在单独的选项卡告知 ,此东西叫payload

image.png

另外 JObject是不支持的,会识别成 整成的get参数,而非payload.

另外关于属性注解

1
2
3
4
5

[HttpPost("AddViewFromItem")]
public String AddViewFromItem
(String title, String tag, int roomID, float scale, String type, [ FromForm]String data,bool debug)
{

除了FromForm 外 其他都是get提交 FromForm提交的是 application/x-www-url-formencoded

FromBody 提交的是application/json
另外

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21


[HttpPost]

public ActionResult<Data> Post([FromForm][FromBody] Data data)

{

return new ActionResult<Data>(data);

}

public class Data

{

public string Id { get; set; }

public string Txt { get; set; }

}

它应该将数据回显给用户,没什么特别的。但是,这两个属性中只有一个有效,具体取决于顺序。

另外 get也可以用json传递也就是payload,但是 不能有formform