forked from siteserver/cms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebApiConfig.cs
More file actions
58 lines (49 loc) · 1.94 KB
/
WebApiConfig.cs
File metadata and controls
58 lines (49 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System.Linq;
using System.Web.Http;
using System.Web.Http.Cors;
using System.Web.Http.Dispatcher;
using System.Web.Routing;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using SiteServer.Utils;
namespace SiteServer.API
{
// https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes(new CentralizedPrefixProvider(WebConfigUtils.ApiPrefix));
config.Services.Replace(typeof(IHttpControllerSelector),
new NamespaceHttpControllerSelector(config));
var corsAttr = new EnableCorsAttribute("*", "*", "*")
{
SupportsCredentials = true
};
config.EnableCors(corsAttr);
RouteTable.Routes.Ignore(""); //Allow index.html to load
var jsonFormatter = config.Formatters.JsonFormatter;
//var settings = new JsonSerializerSettings
//{
// ContractResolver = new CamelCasePropertyNamesContractResolver()
//};
//var timeFormat = new IsoDateTimeConverter
//{
// DateTimeFormat = "yyyy-MM-dd HH:mm:ss"
//};
//settings.Converters.Add(timeFormat);
//jsonFormatter.SerializerSettings = settings;
jsonFormatter.SerializerSettings = TranslateUtils.JsonSettings;
jsonFormatter.Indent = true;
var formatters = config.Formatters.Where(formatter =>
formatter.SupportedMediaTypes.Any(media => media.MediaType == "application/xml"))
.ToList();
foreach (var match in formatters)
{
config.Formatters.Remove(match);
}
config.EnsureInitialized();
}
}
}