88 lines
3.1 KiB
C#
88 lines
3.1 KiB
C#
using Newtonsoft.Json;
|
||
using Newtonsoft.Json.Serialization;
|
||
using NX.ERP.WebAPi.Filters;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Net.Http;
|
||
using System.Net.Http.Formatting;
|
||
using System.Net.Http.Headers;
|
||
using System.Threading;
|
||
using System.Threading.Tasks;
|
||
using System.Web.Http;
|
||
using System.Web.Http.Controllers;
|
||
using System.Web.Http.Cors;
|
||
using System.Web.Http.Filters;
|
||
|
||
namespace NX.ERP.WebAPi
|
||
{
|
||
|
||
public class JsonContentNegotiator : IContentNegotiator
|
||
{
|
||
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)
|
||
{
|
||
|
||
|
||
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"];
|
||
|
||
|
||
//optional: set serializer settings here
|
||
|
||
//System.Web.Http.Filters.IExceptionFilter
|
||
config.Filters.Add(new ExceptionFilter());
|
||
config.Filters.Add(new ApiResultAttribute());
|
||
Console.WriteLine(config.Filters.Count);
|
||
config.Formatters.JsonFormatter.SerializerSettings = new JsonSerializerSettings
|
||
{
|
||
//忽略循环引用,如果设置为Error,则遇到循环引用的时候报错
|
||
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
|
||
DateTimeZoneHandling=DateTimeZoneHandling.Local,
|
||
|
||
//NullValueHandling = NullValueHandling.Ignore,
|
||
//Formatting = Newtonsoft.Json.Formatting.Indented,
|
||
//小驼峰命名法
|
||
ContractResolver = new DefaultContractResolver()
|
||
{
|
||
|
||
NamingStrategy = new SnakeCaseNamingStrategy()
|
||
}
|
||
//格式化日期时间
|
||
|
||
//空值处理
|
||
//NullValueHandling = NullValueHandling.Ignore
|
||
|
||
};
|
||
config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
|
||
|
||
// Web API 路由
|
||
config.MapHttpAttributeRoutes();
|
||
|
||
config.Routes.MapHttpRoute(
|
||
name: "DefaultApi",
|
||
routeTemplate: "api/{controller}/{action}/{code}",
|
||
defaults: new { code = RouteParameter.Optional }
|
||
);
|
||
|
||
}
|
||
}
|
||
}
|