これは私のモデルです。ASP.NET Core 2 WebAPIのPOSTリクエストでモデルバインディングが機能しないWebAPI
public class Patient
{
public string Name { get; set; }
public string Gender { get; set; }
public double Age { get; set; }
public DateTime DateOfBirth { get; set; }
public string MobileNumber { get; set; }
public string Address { get; set; }
public string Occupation { get; set; }
public string BloodGroup { get; set; }
}
によって傍受され、これが私のコントローラです。
[Produces("application/json")]
[Route("api/Patient")]
public class PatientController : Controller
{
[HttpPost]
public IActionResult Post([FromBody] Patient patient)
{
//Do something with patient
return Ok();
}
}
私の問題は、私はいつも[FromBody] Patient patient
patient
ため
null
EDIT 1取得していますされています をイングバーさんのコメントによると、私は次のようなリクエストボディのJSONを作った:
{patient: {"name":"Leonardo","gender":"",....,"bloodGroup":""}}
しかし今回はプロパティのデフォルト値をゲートします(例:name: ""
とage: 0
)
EDIT 2: Startup.csマイConfigureServices
とConfigure
方法は、私は答えを見つけたいくつかの髪を失った後
public IServiceProvider ConfigureServices(IServiceCollection services)
{
services.AddCors();
services.AddMvc();
var containerBuilder = new ContainerBuilder();
containerBuilder.RegisterType<PatientRepository>().As<IPatientRepository>();
containerBuilder.RegisterType<UnitOfWork>()
.As<IUnitOfWork>()
.WithParameter("connectionString", Configuration.GetConnectionString("PostgreConnection"));
containerBuilder.Populate(services);
var container = containerBuilder.Build();
return new AutofacServiceProvider(container);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors(corsPolicyBuilder =>
corsPolicyBuilder.WithOrigins("http://localhost:3000")
.AllowAnyMethod()
.AllowAnyHeader());
app.UseMvc();
}
おそらく、あなたは{patient:{name: "a" ...}}のように、apiコントローラを派生させたり、jsonのようにする必要があります。あなたはこれを試しましたか? – ingvar
私はやったし、私の編集セクションを見てください。 – Towhid
大文字のプロパティ(名前の代わりに名前など)で動作します。また、PatientController:Controllerの代わりにPatientController:ApiControllerを試しましたか? afaik [frombody]はapicontrollerのためだけに働きます – ingvar