前台用的post加payloa的方式提交的json .net core 3.2这里cxmodel是收不到参数的。
[HttpPost]
public AjaxRes AjaxChaXunKuCunLiang([FromBody] JObject cxmodel)
{
string keyword = cxmodel.Value<string>("keyword");
string KuWei = cxmodel.Value<string>("KuWei");
}
解决方案如下。
Nuget 引入 Microsoft.AspNetCore.Mvc.NewtonsoftJson
Startup.cs
using Microsoft.AspNetCore.Mvc.NewtonsoftJson; //解决JObject接收参数的问题
using Newtonsoft.Json.Serialization; //解决JObject接收参数的问题
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
//解决JObject接收参数的问题 start
services.AddControllers().AddNewtonsoftJson(options =>
{
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
//解决JObject接收参数的问题
}
}