I've tried all the solutions I've found, but most don't apply to my issue. I am sending post requests using JSON, and up to 3.0 they worked fine.
You can see in my controller I have [FromBody]
and my model has properties. Those were the two issues I saw from other people pop up. You can also see my request values match the name of my model and there are none missing. I can't find anything else that might be causing this issue.
Edit - Based on the comment below I checked the Json rules in place. Initially the default rule was in place that made the first character of value names lower case. It does that on the way down, but I don't know how to tell if that is affecting it when sending values to the server. Is there a breakpoint I can put in somewhere to see the post values before they are sent to the controller?
Edit2 - Changing startup to this makes it so the model isn't null, but all the values are default. Strings are null, ints are 0.
services.AddControllers().AddJsonOptions(o =>
{
o.JsonSerializerOptions.PropertyNameCaseInsensitive = false;
});
It looks like most of the json options to do with name changing don't affect deserializing from what I can see.
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.AddCors();
services.AddControllers();
services.AddAuthentication(x =>
{
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(x =>
{
x.RequireHttpsMetadata = false;
x.SaveToken = true;
x.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
IssuerSigningKey = new SymmetricSecurityKey(key),
ValidateIssuer = false,
ValidateAudience = false
};
});
//services.AddMvc();
services.AddTransient<AuthService>();
services.AddTransient<LocationService>();
services.AddTransient<MessageService>();
services.AddTransient<AutoAssignService>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseAuthentication();
app.UseRouting();
// global cors policy
app.UseCors(x => x
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader());
app.UseAuthorization();
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
});
}
}
Request Model
public class CensusManagementSearch
{
public int LocationId { get; set; }
public int PracticeId { get; set; }
public string Name { get; set; }
public int StatusId { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public bool Print { get; set; }
}
My controller method
[HttpPost]
public IActionResult PostCensusManagement([FromBody] CensusManagementSearch search)
{
}