asp.net core webapi处理Post请求中的request payload
request payload的Content-Type实际上是text/plain的,如果请求的 Content-Type
为 application/json
,这将导致415 Unsupported Media Type HTTP error。
有两个解决方法
1使用 application/json
Content-Type采用application/json并确保 request payload是有效的json格式,比如
{
"cookie": "value"
}
Then the action signature needs to accept an object with the same shape as the JSON object.
创建实体作为接收参数
public class CookieWrapper
{
public string Cookie { get; set; }
} ... public IActionResult GetRankings([FromBody] CookieWrapper c)
或者使用
dynamic、
Dictionary
public IActionResult GetRankings([FromBody] dynamic c) public IActionResult GetRankings([FromBody] Dictionary<string, string> c)
2使用 text/plain
客户端请求使用 Content-Type
: text/plain,
服务端添加TextPlainInputFormatter
public class TextPlainInputFormatter : TextInputFormatter
{
public TextPlainInputFormatter()
{
SupportedMediaTypes.Add("text/plain");
SupportedEncodings.Add(UTF8EncodingWithoutBOM);
SupportedEncodings.Add(UTF16EncodingLittleEndian);
} protected override bool CanReadType(Type type)
{
return type == typeof(string);
} public override async Task<InputFormatterResult> ReadRequestBodyAsync(
InputFormatterContext context,
Encoding encoding)
{
string data = null;
using (var streamReader = context.ReaderFactory(
context.HttpContext.Request.Body,
encoding))
{
data = await streamReader.ReadToEndAsync();
} return InputFormatterResult.Success(data);
}
}
并在Startup.cs配置mvc
services.AddMvc(options =>
{
options.InputFormatters.Add(new TextPlainInputFormatter());
});
翻译自https://stackoverflow.com/questions/41798814/asp-net-core-api-post-parameter-is-always-null
作者:B.it
技术收录网站:核心技术(http://www.coretn.cn)
本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。
asp.net core webapi处理Post请求中的request payload的更多相关文章
- ASP.NET Core 防止跨站请求伪造(XSRF/CSRF)攻击 (转载)
什么是反伪造攻击? 跨站点请求伪造(也称为XSRF或CSRF,发音为see-surf)是对Web托管应用程序的攻击,因为恶意网站可能会影响客户端浏览器和浏览器信任网站之间的交互.这种攻击是完全有可能的 ...
- ASP.NET Core WebAPI中的分析工具MiniProfiler
介绍 作为一个开发人员,你知道如何分析自己开发的Api性能么? 在Visual Studio和Azure中, 我们可以使用Application Insight来监控项目.除此之外我们还可以使用一个免 ...
- 第十九节:Asp.Net Core WebApi基础总结和请求方式
一. 基础总结 1.Restful服务改造 Core下的WebApi默认也是Restful格式服务,即通过请求方式(Get,post,put,delete)来区分请求哪个方法,请求的URL中不需要写方 ...
- .NET Core:在ASP.NET Core WebApi中使用Cookie
一.Cookie的作用 Cookie通常用来存储有关用户信息的一条数据,可以用来标识登录用户,Cookie存储在客户端的浏览器上.在大多数浏览器中,每个Cookie都存储为一个小文件.Cookie表示 ...
- ASP.NET Core WebApi 返回统一格式参数(Json 中 Null 替换为空字符串)
相关博文:ASP.NET Core WebApi 返回统一格式参数 业务场景: 统一返回格式参数中,如果包含 Null 值,调用方会不太好处理,需要替换为空字符串,示例: { "respon ...
- Asp.Net Core WebApi中接入Swagger组件(初级)
开发WebApi时通常需要为调用我们Api的客户端提供说明文档.Swagger便是为此而存在的,能够提供在线调用.调试的功能和API文档界面. 环境介绍:Asp.Net Core WebApi + S ...
- Asp.Net Core WebAPI+PostgreSQL部署在Docker中
PostgreSQL是一个功能强大的开源数据库系统.它支持了大多数的SQL:2008标准的数据类型,包括整型.数值值.布尔型.字节型.字符型.日期型.时间间隔型和时间型,它也支持存储二进制的大对像, ...
- asp.net core webapi之跨域(Cors)访问
这里说的跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协议.域名.端口有任何一个不同,都被当作 ...
- Asp.net Core WebApi 使用Swagger做帮助文档,并且自定义Swagger的UI
WebApi写好之后,在线帮助文档以及能够在线调试的工具是专业化的表现,而Swagger毫无疑问是做Docs的最佳工具,自动生成每个Controller的接口说明,自动将参数解析成json,并且能够在 ...
随机推荐
- μCOS-Ⅲ——常用注意事项
**1,**main函数在调用其他函数之前必须先调用OSInit()函数对内核进行初始化. 2,所有的错误类型码都以OS_ERR_为前缀, 3,命名时尽量统一个格式,所有的函数.变量.宏定义和#def ...
- Java randomString
public static String randomString(int strLength) { Random rnd = ThreadLocalRandom.current(); StringB ...
- java 两行代码解析json文件
public void JsonRead() throws IOException{ String str = FileUtils.readFileToString(new File("D: ...
- .NET复习笔记-泛型
1.yield关键字用于返回迭代器具体的值,如下框代码所示 /// 返回0~9整数集合 public static IEnumerable<int> yieldSampleMethod() ...
- spring cloud config git库文件搜索顺序
spring.cloud.config.server.git.uri只配置到仓库那一层就行了,需要访问仓库的子目录的话就配置spring.cloud.config.server.git.searchP ...
- centos7 十万并发 关健配置
vim /etc/sysctl.conf,添加 --开机启动加载内核参数:fs.file-max = 65535kernel.sem=250 32000 100 128 /proc/sys/kerne ...
- 工具类:mybatis中使用Threadlocal开启session及关闭session
1.线程容器,给线程绑定一个Object 内容,后只要线程不变,可以随时取出. 1.1 改变线程,无法取出内容. final ThreadLocal threadLocal = new ThreadL ...
- Linux 驱动——Button驱动5(atomic)原子量
button_drv.c驱动文件: #include <linux/module.h>#include <linux/kernel.h>#include <linux/f ...
- 城市扩张实验---ARCGIS处理踩雷
1. 将每个坐标点按照所属泰森多边形分类后,得到了数据统计表,导出到excel里面数据透析. 数据透析行代表泰森多边形编号,列代表地物类别,求和项是frequency.一开始的求和不会显示求和,,而是 ...
- CSS组合设计输入框和按钮生成自定义关键字查询栏
效果图: html代码: <!DOCTYPE html> <head> <title></title> </head> <body&g ...