Files
U8-Import/NX.ERP.WebAPi/App_Start/WebApiConfig.cs

117 lines
4.3 KiB
C#
Raw Normal View History

2021-01-09 16:36:17 +08:00
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
2021-01-05 11:11:28 +08:00
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
2021-01-13 12:37:27 +08:00
using System.Threading;
using System.Threading.Tasks;
2021-01-05 11:11:28 +08:00
using System.Web.Http;
2021-01-13 12:37:27 +08:00
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
2021-01-05 11:11:28 +08:00
namespace NX.ERP.WebAPi
{
2021-01-13 12:37:27 +08:00
public class ExceptionFilter : System.Web.Http.Filters.IExceptionFilter
{
public bool AllowMultiple => throw new NotImplementedException();
public Task ExecuteExceptionFilterAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
{
var ex = actionExecutedContext.Exception;
actionExecutedContext.Response = new System.Net.Http.HttpResponseMessage();
actionExecutedContext.Response.Content = new System.Net.Http.ObjectContent(typeof(ResultModel<string>),
new ResultModel<string> { ErrCode = 500, ErrMsg = ex.Message, Data = null },
new JsonMediaTypeFormatter());
return Task.FromResult(0);
}
}
public class ApiResultAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
base.OnActionExecuted(actionExecutedContext);
if (actionExecutedContext.Exception == null)
{
ResultModel<object> result = new ResultModel<object>();
// 取得由 API 返回的状态代码
result.ErrCode = 0;
// 取得由 API 返回的资料
result.Data = actionExecutedContext.ActionContext.Response.Content.ReadAsAsync<object>().Result;
//请求是否成功
// result.IsSuccess = actionExecutedContext.ActionContext.Response.IsSuccessStatusCode;
//结果转为自定义消息格式
HttpResponseMessage httpResponseMessage = new HttpResponseMessage() { Content = new StringContent(JsonConvert.SerializeObject(result, GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings)) };
// 重新封装回传格式
actionExecutedContext.Response = httpResponseMessage;
}
}
}
2021-01-05 11:11:28 +08:00
public class JsonContentNegotiator : IContentNegotiator
{
2021-01-13 12:37:27 +08:00
2021-01-05 11:11:28 +08:00
private readonly JsonMediaTypeFormatter _jsonFormatter;
public JsonContentNegotiator(JsonMediaTypeFormatter formatter)
{
_jsonFormatter = formatter;
}
public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
{
var result = new ContentNegotiationResult(_jsonFormatter, new MediaTypeHeaderValue("application/json"));
return result;
}
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
2021-01-13 12:37:27 +08:00
//System.Web.Http.Filters.IExceptionFilter
config.Filters.Add(new ExceptionFilter());
config.Filters.Add(new ApiResultAttribute());
Console.WriteLine(config.Filters.Count);
2021-01-05 11:11:28 +08:00
// Web API 配置和服务
2021-01-09 16:36:17 +08:00
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings
{
//忽略循环引用,如果设置为Error,则遇到循环引用的时候报错
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
Formatting = Newtonsoft.Json.Formatting.Indented,
//小驼峰命名法
2021-01-15 09:26:17 +08:00
//ContractResolver = new DefaultContractResolver()
//{
2021-01-09 16:36:17 +08:00
2021-01-15 09:26:17 +08:00
// NamingStrategy = new SnakeCaseNamingStrategy()
//}
2021-01-09 16:36:17 +08:00
//格式化日期时间
2021-01-13 12:37:27 +08:00
2021-01-09 16:36:17 +08:00
//空值处理
//NullValueHandling = NullValueHandling.Ignore
};
2021-01-05 11:11:28 +08:00
// Web API 路由
config.MapHttpAttributeRoutes();
2021-01-13 12:37:27 +08:00
2021-01-05 11:11:28 +08:00
config.Routes.MapHttpRoute(
name: "DefaultApi",
2021-01-09 16:36:17 +08:00
routeTemplate: "api/{controller}/{action}/{code}",
defaults: new { code = RouteParameter.Optional }
2021-01-05 11:11:28 +08:00
);
}
}
}