.Net Core 5下WebAPI FromForm 及FromBody FromQuery使用方式,有图有真相

https://blog.csdn.net/hamunet/article/details/120968882

  1. FromForm POST方式
    后端代码:
    [HttpPost("Tel/FromFormTest")]
    public async Task<ActionResult<ResponseMessageWrap>> FromFormTest([FromForm] TelOutPos telOutPos)
    {
    await Task.CompletedTask;
    return new ResponseMessageWrap {Data= telOutPos.agNo };
    }

Js请求:
1、 Content-Type:application/x-www-form-urlencoded
var settings = {
"url": "http://localhost:5003/Tel/FromFormTest",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/x-www-form-urlencoded"
},
"data": {
"regId": "123131",
"token": "123123",
"agNo": "123213",
"bindTel": "2222"
}
};

$.ajax(settings).done(function (response) {
console.log(response);
});

2、 Content-Type:multipart/form-data
var form = new FormData();
form.append("regId", "11");
form.append("token", "22");

var settings = {
"url": "http://localhost:5003/Tel/FromFormTest",
"method": "POST",
"timeout": 0,
"processData": false,
"mimeType": "multipart/form-data",
"contentType": false,
"data": form
};

$.ajax(settings).done(function (response) {
console.log(response);
});

3、Content-Type:application/json 对象值传不过去

总结:
1、接口参数为对象:只能key value方式对象传递,后台可以接收到,符合以上3中方式
2、接口参数分别接受,url方式传值不考虑Content-Type 类型都可以接受到的
...FromFormTest1?regId=13&token=1213&agNo=123213&bindTel=123123

后台代码 Dictionary 为key value 集合:
public static async Task<(bool, string)> HttpPostAsyncOptimizeFormUrl(string requestUrl, Dictionary<string, string> dicUrl)
{
try
{
using (HttpClient httpClient = new HttpClient())
{
StringBuilder builder = new StringBuilder();
int i = 0;
foreach (var item in dicUrl)
{
if (i > 0)
builder.Append("&");
builder.AppendFormat("{0}={1}", item.Key, item.Value);
i++;
}
var responseResult = httpClient.PostAsync(requestUrl, new StringContent(builder.ToString(), System.Text.Encoding.UTF8, "application/x-www-form-urlencoded")).Result;
if (responseResult.StatusCode == HttpStatusCode.OK)
{
return (true, await responseResult.Content.ReadAsStringAsync());
}
else
{
return (false, $"接口地址返回错误状态码:{responseResult.StatusCode}");
}

            }
        }

2.FromBody POST方式
后台代码:
[HttpPost("Tel/FromBodyTest")]
public async Task<ActionResult<ResponseMessageWrap>> FromBodyTest([FromBody] TelOutPos telOutPoso)
{
await Task.CompletedTask;
return new ResponseMessageWrap { Data = telOutPoso.agNo };
}
JS:
var settings = {
"url": "http://localhost:5003/Tel/FromBodyTest",
"method": "POST",
"timeout": 0,
"headers": {
"Content-Type": "application/json"
},
"data": JSON.stringify({
"agNo": "1123"
}),
};

$.ajax(settings).done(function (response) {
console.log(response);
});
只能才作用 "Content-Type": "application/json" 这种方式 ,其他方式一律报media type 错误

3.FromQuery
接口参数对象可以接受URL传值,不接受对象传值,GET方式比较符合这种场景,POST当然也可以
FromQueryTest?egId=13&token=1213&agNo=123213&bindTel=123123
后台代码:
[HttpPost("Tel/FromQueryTest")]
public async Task<ActionResult<ResponseMessageWrap>> FromQueryTest([FromQuery] TelOutPos telOutPos)
{
await Task.CompletedTask;
return new ResponseMessageWrap { Data = telOutPos.agNo };
}

posted @ 2022-03-05 18:24  乌卡拉卡  阅读(723)  评论(0编辑  收藏  举报