【发布时间】:2017-09-25 05:30:54
【问题描述】:
我正在使用 Postman 并尝试在正文中发送 post json 数据。 How Postman request looks like
这是我的 api 方法
[HttpPost("attach")]
[AllowAnonymous]
public async Task<IActionResult> AttachToBuilding([FromBody]AttachmentDto dto)
发送请求后 d 始终为空。 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
}
【问题讨论】:
-
显示你的 json 帖子
-
已添加请求详情
-
您是否在 MVC 中间件之前使用任何中间件,从请求流中读取?因为请求流只是转发并且不能倒带(因为它直接读取从浏览器发送的数据),所以如果您读取数据流,您最终会在流的末尾并且当模型绑定器尝试读取它时,它没有数据
-
不,我不是。我使用的第一个中间件是 MVC
-
@user2148690 你似乎在混合 mvc 和 web api。如果这意味着是 web api,请将
[Route("attach")][HttpPost]替换为[HttpPost("attach")]在此处阅读属性路由以及 docs.microsoft.com/en-us/aspnet/core/mvc/controllers/…
标签: asp.net-core asp.net-core-mvc asp.net-core-1.0 asp.net-core-webapi