.net core 接口,动态接收各类型请求的参数

HttpPost

public async Task<IActionResult> testpost([FromForm] object info)

{
//Postman工具测试结果:
//FromBody,Postman的body只有=raw+json时才进的来
//参数为空时,Body(form-data、x-www-form-urlencoded)解析到的数据也有所不同

Dictionary<string,object> parmsVals=new Dictionary<string,object>();

try

{

if (HttpContext.Request.Body != null)

{

Stream _stream = HttpContext.Request.Body;

//_stream.Position = 0;

using (StreamReader reader = new StreamReader(_stream, Encoding.UTF8))

{

string strv = await reader.ReadToEndAsync();

if (!string.IsNullOrEmpty(strv) && strv.Trim().StartsWith("{") && strv.Trim().EndsWith("}"))

{

parmsVals = JsonConvert.DeserializeObject<Dictionary<string, object>>(strv.Trim());

}

else

{

parmsVals.Add("stream_str", strv);

}

}

}

}

catch { }

if (info != null && JsonConvert.SerializeObject(info).Trim()!="{}")

{

parmsVals.Add("info", JsonConvert.SerializeObject(info));

}

try

{

var files = Request.Form.Files;

if (files != null && files.Count > 0)

{

}

}

catch { }

if (HttpContext.Request.Query.Keys.ToList() != null)

{

List<string> lst = HttpContext.Request.Query.Keys.ToList();

foreach (string key in lst)

{

if (!parmsVals.ContainsKey(key))

{

parmsVals.Add(key, Request.Query[key].ToString().Trim());

}

}

}

try

{

if (HttpContext.Request.Form != null)

{

List<string> lst = HttpContext.Request.Form.Keys.ToList();

foreach (string key in lst)

{

if (!parmsVals.ContainsKey(key))

{

parmsVals.Add(key, Request.Form[key].ToString().Trim());

}

}

}

}

catch(Exception ex)

{

string msg = ex.Message;

}

//Values/testurl

return Content(JsonConvert.SerializeObject(parmsVals));

}

一、Body以 from-data 传参数

二、Body以 x-www-from-urledcoded 传参数

三、Body以 raw+JSON 传参数

相关推荐
武藤一雄20 小时前
C#中常见集合都有哪些?
开发语言·微软·c#·.net·.netcore
武藤一雄2 天前
.NET 中常见计时器大全
microsoft·微软·c#·.net·wpf·.netcore
武藤一雄3 天前
[.NET] 中 System.Collections.Generic命名空间详解
windows·微软·c#·asp.net·.net·.netcore
van久7 天前
.Net Core 学习:Razor Pages中 HTML 表头字段的两种写法对比
学习·html·.netcore
武藤一雄7 天前
C# 万字拆解线程间通讯?
后端·微软·c#·.net·.netcore·多线程
武藤一雄8 天前
.NET中到底什么是SignalR (持续更新)
后端·微软·c#·asp.net·.net·.netcore·signalr
by__csdn8 天前
第二章 (.NET Core环境搭建)第二节( Visual Studio Code)
ide·vscode·c#·vue·asp.net·.net·.netcore
by__csdn8 天前
第二章 (.NET Core环境搭建)第三节( Visual Studio for Mac)
ide·kubernetes·c#·asp.net·.net·.netcore·visual studio
武藤一雄9 天前
C#:进程/线程/多线程/AppDomain详解
后端·微软·c#·asp.net·.net·wpf·.netcore
武藤一雄10 天前
C#:Linq大赏
windows·后端·microsoft·c#·.net·.netcore·linq