gpt4 book ai didi

c# - ASP.NET Core 无法读取请求正文

转载 作者:太空狗 更新时间:2023-10-29 17:43:05 24 4
gpt4 key购买 nike

几周以来,我一直在研究 ASP.NET Core。我试图基于此博客实现一些目标: Microservices

我的project.json如下:

{
"version": "1.0.0-*",
"compilationOptions": {
"emitEntryPoint": true
},

"dependencies": {

"Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
"Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
"Microsoft.AspNet.Diagnostics": "1.0.0-rc1-*",
"Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
"EntityFramework.Core": "7.0.0-rc1-final",
"EntityFramework.Commands": "7.0.0-rc1-final",
"EntityFramework.MicrosoftSqlServer": "7.0.0-rc1-final",
"EntityFramework.MicrosoftSqlServer.Design": "7.0.0-rc1-final",
"Microsoft.AspNet.Mvc.Formatters.Json": "6.0.0-rc1-final",
"Microsoft.AspNet.Mvc.Formatters.Xml": "6.0.0-rc1-final",
"System.Security.Cryptography.Algorithms": "4.0.0-beta-23516"

},

"commands": {
"web": "Microsoft.AspNet.Server.Kestrel",
"ef": "EntityFramework.Commands"
},

"frameworks": {

"dnxcore50": {
"dependencies": {


}

}
},

"exclude": [
"wwwroot",
"node_modules"
],
"publishExclude": [
"**.user",
"**.vspscc"
]
}

Startup.cs中的ConfigureServices方法如下:

public void ConfigureServices(IServiceCollection services)
{
//Registering Authorization Database
AutorizationAccessRegisteration.RegisterComponents(services, Configuration);

services.AddMvcCore()
.AddJsonFormatters(a => a.ContractResolver = new CamelCasePropertyNamesContractResolver());

//Add cors built in support.
services.AddCors();

services.AddMvcCore().AddApiExplorer();

//Add MVC for supporting WebApi requests
#region MVC Add

services.AddMvc();

services.AddMvc().AddMvcOptions(options =>
{
options.RespectBrowserAcceptHeader = true;

// Input Formatters.
options.InputFormatters.Clear();

var jsonInputFormatter = new JsonInputFormatter()
{
SerializerSettings = new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
,
DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate,
NullValueHandling = NullValueHandling.Ignore
}
};


options.InputFormatters.Add(jsonInputFormatter);

//Output formater
//as part of get/post request, set the header Accept = application/json or application/xml
var jsonOutputFormatter = new JsonOutputFormatter();
jsonOutputFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
jsonOutputFormatter.SerializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Ignore;
options.OutputFormatters.Insert(0, jsonOutputFormatter);

options.OutputFormatters.Insert(1, new XmlDataContractSerializerOutputFormatter());

});

#endregion
}

这是我在 Startup.cs 中的 Confiure 方法:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{

}
else if (env.IsStaging())
{

}
else if (env.IsProduction())
{

}

app.UseIISPlatformHandler();

app.UseCors(builder =>
builder.WithOrigins("*").AllowAnyHeader().AllowAnyMethod());

//Middlewares addition to the pipelines:
/// We add the middlewares in the following fashion:
/// - Exception Handler
/// - Logger
/// - Authorization Handler
/// There is a big reason of doing that.
///
app.UseExceptionHandler();
app.UseLoggerHandler();
app.UseAuthorizationHandler();

app.UseMvc();
}

AuthorizationController 如下:

[Route("api/Authorization")]
public class AuthorizationController : Controller
{
.
[HttpPost]
public void Post([FromBody]string value)
{
}
.
}

Post 方法最初有 [FromBody]string[] 值。我通过将其设为简单的 string 类型进一步简化了它。我在 Chrome 上使用 Advance Rest Client 发送 HTTP 请求。当 string[] 是我在 body 中的以下值的类型时:

{

["value","sdjklgsdjlg"]

}

在简化参数后,我尝试使用以下正文发送请求:

{"sdjklgsdjlg"}

也试过这个:

{"value":"sdjklgsdjlg"}

我错过了什么吗?我之前读过,旧的 WebApi 用于将 JSON 映射到复杂对象和普通参数的工作方式,它在 .NET Core 中以类似的方式工作。

另外,我应该详细说明 断点 在所有中间件和 Controller 上正常命中。但是似乎没有一个中间件能够读取 Request 的流相关的东西:

context.Request variable problems

context.Request.Body errors

请告诉我哪里出了问题。非常感谢!

最佳答案

这对我有用:

[HttpPost]
public async Task<IActionResult> CreateApp([FromQuery]string userId)
{
string appDefinition = await new StreamReader(Request.Body).ReadToEndAsync();
var newAppJson = JObject.Parse(appDefinition);
...

关于c# - ASP.NET Core 无法读取请求正文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36082683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com