Web API 的设计有很多傻逼之处. 其中 FromBody 的设计就是最傻逼的设计之一.今天来教大家怎么去掉这个傻逼的设计. Controller依然是继承ApiController API Post 函数直接 去掉所有参数.然后 直接从Request.Content 读取Body内容.var postValue = Request.Content.ReadAsStringAsync().Result;简单粗暴又好用.比FromBody  不知道要强多少倍. 例如:

 

public class XXXController : ApiController 

{

public ApiResult Post() {
var errorMsg = new System.Text.StringBuilder("错误消息");
try {
var jsonList = Request.Content.ReadAsStringAsync().Result; 
} catch (Exception ex) {
return ApiResult.Error(ex.Message);
}
return ApiResult.OK();
}

 

}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-13
  • 2021-09-26
  • 2021-06-03
  • 2021-06-25
猜你喜欢
  • 2022-12-23
  • 2019-01-19
  • 2021-08-10
  • 2022-12-23
  • 2022-12-23
  • 2021-08-08
  • 2022-12-23
相关资源
相似解决方案