.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.Querykey.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.Formkey.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 传参数

相关推荐
1-1=09 天前
C# 数据结构和特殊类型
数据结构·c#·.net·.netcore
1-1=012 天前
C# IO和序列化
c#·.net·.netcore
宝桥南山13 天前
.NET 11 - 尝试一下Blazor中的新特性SupplyParameterFromTempData
microsoft·微软·c#·asp.net·.net·.netcore
1-1=014 天前
C# 特性(Attribute)
c#·.net·.netcore
淡海水15 天前
10-01-互操作-C#与C++边界的内存管理
unity·c#·游戏引擎·.netcore·gc
1-1=015 天前
C# 反射
c#·.net·.netcore
1-1=016 天前
C# 泛型
c#·.net·.netcore
淡海水18 天前
09-01-内存-Unity资源生命周期全景
unity·c#·游戏引擎·.netcore·gc
1-1=018 天前
.NET Core 编译技术
.net·.netcore
1-1=019 天前
.NET Core 文件系统
.net·.netcore·1024程序员节