Я пишу WebApi с OData, и я столкнулся с проблемой.Проблема в том, что контроллеры принимают данные только с учетом регистра.Например, когда я передаю {"name":"London"}
, он дает нулевой объект и ошибку The property 'name' does not exist on type 'Taxes.Service.DataLayer.Models.Municipality'. Make sure to only use property names that are defined by the type.
.Но когда я передаю {"Name":"London"}
, все в порядке - объект создан и значение установлено.На самом деле не знаю, что может быть не так.Спасибо.
BaseController.cs
public class BaseController<T> : ODataController where T : BaseModel
{
protected readonly TaxesContext Context;
public BaseController(TaxesContext context)
{
Context = context;
}
[HttpPost]
public async Task<IActionResult> Post([FromBody]T baseObject)
{
if (!ModelState.IsValid)
{
var errors = ModelState
.SelectMany(x => x.Value.Errors, (y, z) => z.Exception.Message);
return BadRequest(errors);
}
await Context.Set<T>().AddAsync(baseObject);
await Context.SaveChangesAsync();
return Created(baseObject);
}
}
BaseModel.cs
public class BaseModel
{
public int Id { get; set; }
}
Municipality.cs
public class Municipality : BaseModel
{
public string Name { get; set; }
public ICollection<Tax> Taxes { get; set; }
}
Tax.cs
public class Tax : BaseModel
{
[Required]
public int MunicipalityId { get; set; }
[Required]
public virtual Municipality Municipality { get; set; }
[Required]
public virtual TaxFrequency Frequency { get; set; }
[Required]
public virtual DateTime StartDate { get; set; }
[Required]
public virtual DateTime EndDate { get; set; }
[Required]
public virtual double Value { get; set; }
}
Startup.cs
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)
{
services.AddOData();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddDbContext<TaxesContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc(routes =>
{
routes.Select().Expand().Filter().OrderBy().MaxTop(100).Count();
routes.MapODataServiceRoute("odata", "odata", GetEdmModel());
routes.MapRoute("Default", "{controller}/{action=Index}/{id?}");
});
}
private static IEdmModel GetEdmModel ()
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet<DataLayer.Models.Municipality>("Municipalities");
builder.EntitySet<DataLayer.Models.Tax>("Taxes");
var function = builder.Function("MunicipalityWithTax");
function.ReturnsCollectionViaEntitySetPath<DataLayer.Models.Municipality>("Municipalities");
return builder.GetEdmModel();
}
}
Почтальон запрос