【发布时间】:2022-01-24 20:20:19
【问题描述】:
我再次尝试使用控制器的 post 方法,但该方法没有捕获 json 模型。休息,所有类型的标题都在工作,而不是 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?}");
});
}
谁能指导我应该改变什么? 或者我哪里错了?
【问题讨论】:
-
红色波浪线也应该放弃它......这是不正确的json......需要引用属性名称。
-
请edit 分享minimal reproducible example 的问题——特别是您发送的JSON,以及您尝试将JSON 绑定到的c# 数据模型
Department文本 而不是截图。堆栈溢出图像不应用于文本内容,请参阅 Discourage screenshots of code and/or errors 和 Why not upload images of code on SO when asking a question 了解原因。
标签: c# json asp.net-core asp.net-web-api postman