I am really disappointed about all new staff of ASP.NET Core, the reason I cant bind data from request body to my Model (the most ordinary thing). I have checked all my configuration, I have updated all my dependencies and run-time. Unfortunately still stacked with model binding.
This is my api method
[HttpPost("attach")] [AllowAnonymous] public async Task<IActionResult> AttachToBuilding([FromBody]AttachmentDto dto)
After sending request, dto always null
This is my model
public class AttachmentDto { public string Identity { get; set; } public Guid BuildingId { get; set; } [Required] public string FirstName { get; set; } [Required] public string LastName { get; set; } public string MiddleName { get; set; } public string Phone { get; set; } [Required] public string UniqueDeviceId { get; set; } [Required] public int ApartmentNumber { get; set; } [Required] public int AgreementNumber { get; set; } [Required] public int FloorNumber { get; set; } }
My Startup.cs ConfigurationServices method
public void ConfigureServices(IServiceCollection services) { services.AddOptions(); services.AddCors(options => { options.AddPolicy("AllowAnyOrigin", builder => { builder.AllowAnyOrigin(); builder.AllowAnyMethod(); builder.AllowAnyHeader(); }); }); services.AddDbContext<KskEfContext>(options => options.UseSqlServer(Configuration.GetConnectionString(ConnectionName))); services.AddSingleton<IConfiguration>(Configuration); AddDependencies(services); services.AddMvcCore() .AddFormatterMappings() //for aspe.net core need to add explicitly the Api Explorer service (swagger) .AddApiExplorer() .AddDataAnnotations() .AddJsonFormatters(options => options.ContractResolver = new CamelCasePropertyNamesContractResolver()); services.AddAutoMapper(); //Register the Swagger generator, defining one or more Swagger documents services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "KSK api", Version = "v1" }); c.IncludeXmlComments(GetXmlCommentsPath()); }); }
I find my configuration correct and I designed my api method like in previeos ASP.NET WebApi nothing extraordinary. It might be problem with framework version.
My dependencies from project.json (after I updated my dependencies)
{"dependencies": {"Ksk.DataAccess": "1.0.0-*","Ksk.Domain": "1.0.0-*","Microsoft.AspNetCore.Cors": "1.1.1","Microsoft.NETCore.App": {"version": "1.1.1","type": "platform" },"AutoMapper": "6.0.2","AutoMapper.Extensions.Microsoft.DependencyInjection": "2.0.1","Microsoft.ApplicationInsights.AspNetCore": "2.0.0","Microsoft.AspNetCore.Authentication.JwtBearer": "1.1.1","Microsoft.AspNetCore.Mvc": "1.1.2","Microsoft.AspNetCore.Routing": "1.1.1","Microsoft.AspNetCore.Server.IISIntegration": "1.1.1","Microsoft.AspNetCore.Server.Kestrel": "1.1.1","Microsoft.AspNetCore.StaticFiles": "1.1.1","Microsoft.EntityFrameworkCore.Design": "1.1.1","Microsoft.EntityFrameworkCore.SqlServer": "1.1.1","Microsoft.EntityFrameworkCore.Tools": "1.1.0","Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.1","Microsoft.Extensions.Configuration.FileExtensions": "1.1.1","Microsoft.Extensions.Configuration.Json": "1.1.1","Microsoft.Extensions.Logging": "1.1.1","Microsoft.Extensions.Logging.Console": "1.1.1","Microsoft.Extensions.Logging.Debug": "1.1.1","Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.1","Swashbuckle.AspNetCore": "1.0.0" },"tools": {"Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final" },"frameworks": {"netcoreapp1.0": {"imports": ["dotnet5.6","portable-net45+win8" ] } },"buildOptions": {"emitEntryPoint": true,"preserveCompilationContext": true,"xmlDoc": true },"runtimeOptions": {"configProperties": {"System.GC.Server": true } },"publishOptions": {"include": ["wwwroot","**/*.cshtml","appsettings.json","web.config" ] },"scripts": {"postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ] } }
My request details
Accept:*/* Accept-Encoding:gzip, deflate, br Accept-Language:en-US,en;q=0.8 Content-Length:280 Content-Type:application/json Host:localhost:5000 Origin:http://localhost:5000 Proxy-Connection:keep-alive Referer:http://localhost:5000/docs/index.html User-Agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36 Request Payload { "Identity": "AST-25","BuildingId": "","FirstName": "Petr","LastName": "Petrov","MiddleName": "Petrovich","Phone": "+77012223344","UniqueDeviceId": "68753A44-4D6F-1226-9C60-0050E4C00067","ApartmentNumber": 20,"AgreementNumber": 292,"FloorNumber": 5 }