ASP.NET 核心如何从正文绑定数据
ASP.NET Core How to bind data from body
我正在使用 Postman 并尝试在正文中发送 post json 数据。
How Postman request looks like
这是我的api方法
[HttpPost("attach")]
[AllowAnonymous]
public async Task<IActionResult> AttachToBuilding([FromBody]AttachmentDto dto)
发送请求后dto始终为空。
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 方法,就像在以前的 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
}
尝试将 buidingId 的值替换为 null
或 "00000000-0000-0000-0000-000000000000"
,因为控制器无法将空的 string
转换为 Guid
。
我正在使用 Postman 并尝试在正文中发送 post json 数据。 How Postman request looks like
这是我的api方法
[HttpPost("attach")]
[AllowAnonymous]
public async Task<IActionResult> AttachToBuilding([FromBody]AttachmentDto dto)
发送请求后dto始终为空。 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 方法,就像在以前的 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
}
尝试将 buidingId 的值替换为 null
或 "00000000-0000-0000-0000-000000000000"
,因为控制器无法将空的 string
转换为 Guid
。