我创建了一个包含.Net Core 2.0的WebApi服务器解决方案。
首先,我定义了错误使用HttpStatusCode 550的特殊情况。
如果正常情况下是响应头中包含“Content-Type:application / json”的HttpStatusCode 200。
顺便说一句,如果HttpStatusCode 550没有包含在响应头中的“Content-Type:application / json”中。
即使我在解决方案中创建了中间件并执行以下操作,也是如此:
调用PostMan,这将导致HttpStatusCode 200的Json格式情况。 但是,它不是HttpStatusCode 550上的json格式。 所以,在这种情况下,这是破解的韩语。
追加,这在本地windows 10机器上是没问题的。 ubuntu 16.0.4机器, 它只是在ubuntu 16.0.4中的服务器机器中引发。
有没有办法解决它?
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseStaticFiles();
app.UseSwagger();
// Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API");
});
// CORS Middleware
app.UseMiddleware<HttpMiddleware>();
app.UseMvc();
}
public async Task Invoke(HttpContext context)
{
context.Response.ContentType = "application/json";
context.Response.Headers.Add("Access-Control-Allow-Origin", "*");
context.Response.Headers.Add("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, PATCH, DELETE");
context.Response.Headers.Add("Access-Control-Allow-Headers", "X-Requested-With,content-type");
context.Response.Headers.Add("Access-Control-Allow-Credentials", "true");
await _next(context);
}
另外,我将结果值设置如下。
errorresult _errorresult = new errorresult();
JsonResult _ObjectResult;
try
{
:
[ source code ]
:
// success case
// _product is result object
_product.resultcode = "200";
_product.resultmessage = _CommonLogic.GetResponseMessage(_PurchaseMessage);
_product.resultoptional = "";
_product.processtime = Convert.ToString(_stopwatch.ElapsedMilliseconds);
_stopwatch.Stop();
_ObjectResult = _CommonLogic.GetObjectResult(_product, 200);
}
catch(Exception ex)
{
_errorresult = new errorresult();
_errorresult.resultcode = "550";
_errorresult.resultmessage = ex.Message.ToString();
_errorresult.resultoptional = "";
_errorresult.processtime = Convert.ToString(_stopwatch.ElapsedMilliseconds);
_stopwatch.Stop();
_ObjectResult = _CommonLogic.GetObjectResult(_errorresult, 550);
}
public JsonResult GetObjectResult(object obj,int iStatusCode)
{
JsonResult _ObjectResult = new JsonResult(obj);
_ObjectResult.StatusCode = iStatusCode;
// _ObjectResult.ContentTypes.Add(new MediaTypeHeaderValue("application/json; charset=utf-8"));
// _ObjectResult.ContentType = "application/json; charset=utf-8";
return _ObjectResult;
}
感谢您阅读我的问题。