修改API 输出格式

This commit is contained in:
admin
2021-01-16 15:38:29 +08:00
parent 7b37f40f56
commit 46dd037909
10 changed files with 89 additions and 30 deletions

View File

@@ -27,7 +27,7 @@ namespace NX.ERP.WebAPi
actionExecutedContext.Response.Content = new System.Net.Http.ObjectContent(typeof(ResultModel<string>),
new ResultModel<string> { ErrCode = 500, ErrMsg = ex.Message, Data = null },
new JsonMediaTypeFormatter());
GlobalConfiguration.Configuration.Formatters.JsonFormatter);
return Task.FromResult(0);
}
@@ -47,11 +47,11 @@ namespace NX.ERP.WebAPi
result.Data = actionExecutedContext.ActionContext.Response.Content.ReadAsAsync<object>().Result;
//请求是否成功
// result.IsSuccess = actionExecutedContext.ActionContext.Response.IsSuccessStatusCode;
//result.IsSuccess = actionExecutedContext.ActionContext.Response.IsSuccessStatusCode;
//结果转为自定义消息格式
HttpResponseMessage httpResponseMessage = new HttpResponseMessage() { Content = new ObjectContent<ResultModel<object>>(result, new JsonMediaTypeFormatter()) };
actionExecutedContext.Response.Content = new ObjectContent<ResultModel<object>>( result, GlobalConfiguration.Configuration.Formatters.JsonFormatter);
// 重新封装回传格式
actionExecutedContext.Response = httpResponseMessage;
// actionExecutedContext.Response = httpResponseMessage;
}
}
}
@@ -80,9 +80,16 @@ namespace NX.ERP.WebAPi
{
public static void Register(HttpConfiguration config)
{
NX.ERP.U8.U8LoginContext.Instance.AccID = System.Configuration.ConfigurationManager.AppSettings["AccId"];
NX.ERP.U8.U8LoginContext.Instance.PassWord = System.Configuration.ConfigurationManager.AppSettings["Password"];
NX.ERP.U8.U8LoginContext.Instance.UserId = System.Configuration.ConfigurationManager.AppSettings["UserId"];
NX.ERP.U8.U8LoginContext.Instance.Server= System.Configuration.ConfigurationManager.AppSettings["Server"];
//System.Web.Http.Filters.IExceptionFilter
config.Filters.Add(new ExceptionFilter());
config.Filters.Add(new ApiResultAttribute());
//config.Filters.Add(new ApiResultAttribute());
Console.WriteLine(config.Filters.Count);
// Web API 配置和服务
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings
@@ -92,11 +99,11 @@ namespace NX.ERP.WebAPi
NullValueHandling = NullValueHandling.Ignore,
//Formatting = Newtonsoft.Json.Formatting.Indented,
//小驼峰命名法
//ContractResolver = new DefaultContractResolver()
//{
ContractResolver = new DefaultContractResolver()
{
// NamingStrategy = new SnakeCaseNamingStrategy()
//}
NamingStrategy = new SnakeCaseNamingStrategy()
}
//格式化日期时间
//空值处理