我正在使用Postman并尝试在体内发送post json数据。 How Postman request looks like
这是我的api方法
[HttpPost("attach")]
[AllowAnonymous]
public async Task<IActionResult> AttachToBuilding([FromBody]AttachmentDto dto)
发送请求后dto始终为null。 Image result
这是我的模特
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; }
}
我的Startup.cs ConfigurationServices方法
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());
});
}
我发现我的配置正确并且我设计了我的api方法,就像在previeos ASP.NET WebApi中没什么特别的。它可能是框架版本的问题。
来自project.json的依赖项
"dependencies": {
"Microsoft.AspNetCore.Authentication.JwtBearer": "1.0.0",
"Microsoft.AspNetCore.StaticFiles": "1.0.0",
"AutoMapper": "5.2.0",
"AutoMapper.Extensions.Microsoft.DependencyInjection": "1.2.0",
"Ksk.DataAccess": "1.0.0-*",
"Ksk.Domain": "1.0.0-*",
"Microsoft.ApplicationInsights.AspNetCore": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Routing": "1.0.1",
"Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1",
"Microsoft.EntityFrameworkCore.Design": "1.1.0",
"Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
"Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview4-final",
"Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
"Microsoft.Extensions.Configuration.Json": "1.0.0",
"Microsoft.Extensions.Logging": "1.1.0",
"Microsoft.Extensions.Logging.Console": "1.0.0",
"Microsoft.Extensions.Logging.Debug": "1.0.0",
"Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
"Microsoft.AspNetCore.Cors": "1.1.1",
"Microsoft.NETCore.App": {
"version": "1.0.1",
"type": "platform"
},
"Swashbuckle.AspNetCore": "1.0.0-rc1"
},
我的详细信息
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
}