[英]How to submit valid XML in a POST request body to an ASP.NET Core Web API that consumes XML?
[英]Asp Net Core Web Api POST Request.Body always length 0
我正在开发一种服务,该服务将使用HTTP Content-Type:应用程序/八位字节流和Transfer-Encoding:分块接收文件。
我能够使服务器获得请求DisableFormValueModelBinding和RequestSizeLimit 。
但是,当我从Request.Body获取数据时,长度始终为0。
在Framework中,我将使用诸如request.Content.ReadAsStreamAsync();之类的东西。 但这似乎不是Core的选择。
我应该如何从客户端(邮递员)那里获得流的内容?
使用Postman,我尝试了body的binary和form-data选项,但是在到达服务器后都没有得到body。 阅读一些文档后,建议创建一个使用MultipartReader的新格式化程序。 但这一切似乎都基于具有multipart / form-data内容类型,而我没有使用。 我也尝试使用curl发送请求,但结果相同。
Program.cs
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
启动文件
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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
}
}
控制者
[HttpPost]
[RequestSizeLimit(2147483648)] // https://stackoverflow.com/questions/43305220/form-key-or-value-length-limit-2048-exceeded
[DisableFormValueModelBinding] // https://dotnetcoretutorials.com/2017/03/12/uploading-files-asp-net-core/
public void Upload()
{
Request.EnableRewind();
Stream body = Request.Body;
Debug.WriteLine(body.Length);
}
我阅读了您从中获得[DisableFormValueModelBinding]
文章 。 该属性的全部目的是阻止ASP.NET读取正文。 因此,为什么body.Length
为0是有意义的。它根本还没有被读取(并且直到您读完整个内容,您才知道长度)。
但是您可以读取请求的Content-Length
标头:
var length = Request.ContentLength;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.