.net Core 3.1里的api,使用JObject当做接收参数的教程
一、引用nuget
Microsoft.AspNetCore.Mvc.NewtonsoftJson
二、在StartUp里增加代码
public void ConfigureServices(IServiceCollection services) { services.AddCors(options => { options.AddPolicy("default", policy => { policy.WithOrigins("*") .AllowAnyHeader() .AllowAnyMethod(); }); }); services.AddControllers().AddNewtonsoftJson(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); }); JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); services.AddAuthorization(); }
三、项目里使用
[HttpPost] public ReturnResult AddUser([FromBody] JObject value) { var returnResult = new ReturnResult(); returnResult = _userLogic.Add(value); return returnResult; }