【发布时间】:2016-03-18 11:01:30
【问题描述】:
几周以来,我一直在研究 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[] value。我通过使其成为简单的string 类型进一步简化了它。我在 Chrome 上使用 Advance Rest Client 发送HTTP request。当string[] 是我的类型时,正文中的值如下:
{
["value","sdjklgsdjlg"]
}
简化参数后,我尝试使用以下正文发送请求:
{"sdjklgsdjlg"}
也试过了:
{"value":"sdjklgsdjlg"}
我错过了什么吗?我之前读过,旧的 WebApi 过去使用 JSON 映射到复杂对象和普通参数的工作方式,它在 .NET Core 中以类似的方式工作。
另外我应该详细说明 breakpoint 在所有中间件和控制器上正常命中。但似乎没有一个中间件能够读取 Request 的流相关内容:
请告诉我哪里出了问题。非常感谢!
【问题讨论】:
-
您的网络嗅探器对此有何看法?你可以粘贴http会话的捕获吗?另外,你能粘贴整个错误信息吗?
-
什么意思?我正在使用 Advance Rest 客户端。如上所述,它显示了正确的请求标头和原始正文。
-
你试过没有 [FromBody] 吗?我认为没有那个它会起作用
-
我以前试过,现在又试了。但是您不认为如果该值没有显示在控制器中,那么这个
[FromBody]可能会出现问题。但事实并非如此。我发布了两张图片,这是我的第一个 ExceptionMiddleware,您可以看到请求的 Body 和 Form 变量中显示了哪些错误。 -
你加了两次MVC,正常吗?并且 MvcCore 也被添加了两次。看起来很奇怪
标签: c# asp.net-core asp.net-core-mvc asp.net-core-1.0