API post 未检测原始 json 模型
API post not decting raw json model
再一次,我试图点击控制器的 post 方法,但该方法没有捕捉到 json 模型。休息,所有类型的 headers 都在工作,而不是 json post.
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
readonly string CorsPolicy = "MyPolicy";
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add Cors
services.AddCors(o => o.AddPolicy(CorsPolicy, builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));
services.AddControllersWithViews();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseCors(CorsPolicy);
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
任何人都可以指导我需要更改什么吗?
或者我哪里错了?
尝试匹配字段名称的大小写,并包含引号,例如"DepartmentId": 0
并确认请求有 Content-Type: application/json
由于您使用的是 json ,因此您必须将 [FromBody] 添加到操作中
pubic ActionResult Post([FromBody] department)
同时在 postman
中修复 json
{
"DepartmentId": 0,
"DepartmentName": "bpu"
}
还支持不区分大小写字母将这些选项添加到启动
using Newtonsoft.Json.Serialization;
.....
services.AddControllersWithViews()
.AddNewtonsoftJson(options =>
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver());
再一次,我试图点击控制器的 post 方法,但该方法没有捕捉到 json 模型。休息,所有类型的 headers 都在工作,而不是 json post.
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
readonly string CorsPolicy = "MyPolicy";
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add Cors
services.AddCors(o => o.AddPolicy(CorsPolicy, builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));
services.AddControllersWithViews();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseCors(CorsPolicy);
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}
任何人都可以指导我需要更改什么吗? 或者我哪里错了?
尝试匹配字段名称的大小写,并包含引号,例如"DepartmentId": 0
并确认请求有 Content-Type: application/json
由于您使用的是 json ,因此您必须将 [FromBody] 添加到操作中
pubic ActionResult Post([FromBody] department)
同时在 postman
中修复 json{
"DepartmentId": 0,
"DepartmentName": "bpu"
}
还支持不区分大小写字母将这些选项添加到启动
using Newtonsoft.Json.Serialization;
.....
services.AddControllersWithViews()
.AddNewtonsoftJson(options =>
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver());