.Net WebApi returns 非开发环境格式的Jsons
.Net WebApi returns Jsons with formats other than the development environment
我们有一个 WebApi,它只是 returns 一个像这样的 JSON 响应:
[{ "company": "KYC" }]
当我们发布 WebApi 时,使用 VS 上的发布选项或者只是在服务器上复制和粘贴,returns这很奇怪,而且它只在这个服务器上。
生产服务器上的响应是:
{ "Version": {
"_Major": 1,
"_Minor": 1,
"_Build": -1,
"_Revision": -1 }, "Content": {
"Headers": [
{
"Key": "Content-Type",
"Value": [
"application/json; charset=utf-8"
]
}
] }, "StatusCode": 200, "ReasonPhrase": "OK", "Headers": [], "RequestMessage": null, "IsSuccessStatusCode":
true }
我已经 "Googled" 这没有成功。
这是控制器代码:
[Authorize]
[HttpGet]
[EnableCors(origins: "*", headers: "*", methods: "*")]
[Route("api/Infra/GetEmpresas/{system}")]
public HttpResponseMessage GetCompanies(string system)
{
logger.Debug("Start GetCompanies");
HttpResponseMessage response = new HttpResponseMessage();
try
{
if (!String.IsNullOrEmpty(system))
{
Companies comps = new Companies();
var result = comps.GetCompanies(system);
if (result != string.Empty)
{
if (result.ToLower().StartsWith("error"))
{
response.StatusCode = HttpStatusCode.NotFound;
response.Content = new StringContent(
"{\"error\":\"" + result + "\"}", System.Text.Encoding.UTF8, "application/json");
}
else
{
response.Content = new StringContent(
result, System.Text.Encoding.UTF8, "application/json");
}
}
else
{
response.StatusCode = HttpStatusCode.NotFound;
response.Content = new StringContent(
"{\"error\":\"Company not found\"}", System.Text.Encoding.UTF8, "application/json");
}
}
else
{
response.StatusCode = HttpStatusCode.NotFound;
response.Content = new StringContent(
"{\"error\":\"System was not provided\"}", System.Text.Encoding.UTF8, "application/json");
}
}
catch (Exception exx)
{
logger.Error("Error on GetCompanies");
logger.Error(exx);
response.StatusCode = HttpStatusCode.InternalServerError;
response.Content = new StringContent(
"{\"error\":\"Error on GetCompanies\"}", System.Text.Encoding.UTF8, "application/json");
}
return response;
}
您是否尝试过使用请求的 CreateResponse/CreateErrorResponse 方法生成 HttpResponseMessage?
[Authorize]
[HttpGet]
[EnableCors(origins: "*", headers: "*", methods: "*")]
[Route("api/Infra/GetEmpresas/{system}")]
public HttpResponseMessage GetCompanies(string system)
{
logger.Debug("Start GetCompanies");
HttpResponseMessage response;
try
{
if (!String.IsNullOrEmpty(system))
{
Companies comps = new Companies();
var result = comps.GetCompanies(system);
if (result != string.Empty)
{
if (result.ToLower().StartsWith("error"))
{
response = Request.CreateErrorResponse(HttpStatusCode.NotFound, result);
}
else
{
response = Request.CreateResponse(result);
}
}
else
{
response = Request.CreateErrorResponse(HttpStatusCode.NotFound, "Company not found");
}
}
else
{
response = Request.CreateErrorResponse(HttpStatusCode.NotFound, "System was not provided");
}
}
catch (Exception exx)
{
logger.Error("Error on GetCompanies");
logger.Error(exx);
response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error on GetCompanies");
}
return response;
}
我的案例的解决方案非常简单。刚刚将框架更新到最新的 .Net 版本,在本例中为 4.8。
我们遇到问题的服务器是 4.6.1586。
我们有一个 WebApi,它只是 returns 一个像这样的 JSON 响应:
[{ "company": "KYC" }]
当我们发布 WebApi 时,使用 VS 上的发布选项或者只是在服务器上复制和粘贴,returns这很奇怪,而且它只在这个服务器上。
生产服务器上的响应是:
{ "Version": { "_Major": 1, "_Minor": 1, "_Build": -1, "_Revision": -1 }, "Content": { "Headers": [ { "Key": "Content-Type", "Value": [ "application/json; charset=utf-8" ] } ] }, "StatusCode": 200, "ReasonPhrase": "OK", "Headers": [], "RequestMessage": null, "IsSuccessStatusCode": true }
我已经 "Googled" 这没有成功。 这是控制器代码:
[Authorize]
[HttpGet]
[EnableCors(origins: "*", headers: "*", methods: "*")]
[Route("api/Infra/GetEmpresas/{system}")]
public HttpResponseMessage GetCompanies(string system)
{
logger.Debug("Start GetCompanies");
HttpResponseMessage response = new HttpResponseMessage();
try
{
if (!String.IsNullOrEmpty(system))
{
Companies comps = new Companies();
var result = comps.GetCompanies(system);
if (result != string.Empty)
{
if (result.ToLower().StartsWith("error"))
{
response.StatusCode = HttpStatusCode.NotFound;
response.Content = new StringContent(
"{\"error\":\"" + result + "\"}", System.Text.Encoding.UTF8, "application/json");
}
else
{
response.Content = new StringContent(
result, System.Text.Encoding.UTF8, "application/json");
}
}
else
{
response.StatusCode = HttpStatusCode.NotFound;
response.Content = new StringContent(
"{\"error\":\"Company not found\"}", System.Text.Encoding.UTF8, "application/json");
}
}
else
{
response.StatusCode = HttpStatusCode.NotFound;
response.Content = new StringContent(
"{\"error\":\"System was not provided\"}", System.Text.Encoding.UTF8, "application/json");
}
}
catch (Exception exx)
{
logger.Error("Error on GetCompanies");
logger.Error(exx);
response.StatusCode = HttpStatusCode.InternalServerError;
response.Content = new StringContent(
"{\"error\":\"Error on GetCompanies\"}", System.Text.Encoding.UTF8, "application/json");
}
return response;
}
您是否尝试过使用请求的 CreateResponse/CreateErrorResponse 方法生成 HttpResponseMessage?
[Authorize]
[HttpGet]
[EnableCors(origins: "*", headers: "*", methods: "*")]
[Route("api/Infra/GetEmpresas/{system}")]
public HttpResponseMessage GetCompanies(string system)
{
logger.Debug("Start GetCompanies");
HttpResponseMessage response;
try
{
if (!String.IsNullOrEmpty(system))
{
Companies comps = new Companies();
var result = comps.GetCompanies(system);
if (result != string.Empty)
{
if (result.ToLower().StartsWith("error"))
{
response = Request.CreateErrorResponse(HttpStatusCode.NotFound, result);
}
else
{
response = Request.CreateResponse(result);
}
}
else
{
response = Request.CreateErrorResponse(HttpStatusCode.NotFound, "Company not found");
}
}
else
{
response = Request.CreateErrorResponse(HttpStatusCode.NotFound, "System was not provided");
}
}
catch (Exception exx)
{
logger.Error("Error on GetCompanies");
logger.Error(exx);
response = Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Error on GetCompanies");
}
return response;
}
我的案例的解决方案非常简单。刚刚将框架更新到最新的 .Net 版本,在本例中为 4.8。 我们遇到问题的服务器是 4.6.1586。