forked from JSONAPIdotNET/JSONAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsonApiHttpConfiguration.cs
More file actions
51 lines (45 loc) · 2.27 KB
/
Copy pathJsonApiHttpConfiguration.cs
File metadata and controls
51 lines (45 loc) · 2.27 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
using System;
using System.Web.Http;
using JSONAPI.ActionFilters;
using JSONAPI.Json;
namespace JSONAPI.Configuration
{
/// <summary>
/// Configures an HttpConfiguration object for use with JSONAPI.NET
/// </summary>
public class JsonApiHttpConfiguration
{
private readonly JsonApiFormatter _formatter;
private readonly FallbackDocumentBuilderAttribute _fallbackDocumentBuilderAttribute;
private readonly JsonApiExceptionFilterAttribute _jsonApiExceptionFilterAttribute;
/// <summary>
/// Creates a new configuration
/// </summary>
public JsonApiHttpConfiguration(JsonApiFormatter formatter,
FallbackDocumentBuilderAttribute fallbackDocumentBuilderAttribute,
JsonApiExceptionFilterAttribute jsonApiExceptionFilterAttribute)
{
if (formatter == null) throw new ArgumentNullException("formatter");
if (fallbackDocumentBuilderAttribute == null) throw new ArgumentNullException("fallbackDocumentBuilderAttribute");
if (jsonApiExceptionFilterAttribute == null) throw new ArgumentNullException("jsonApiExceptionFilterAttribute");
_formatter = formatter;
_fallbackDocumentBuilderAttribute = fallbackDocumentBuilderAttribute;
_jsonApiExceptionFilterAttribute = jsonApiExceptionFilterAttribute;
}
/// <summary>
/// Applies the running configuration to an HttpConfiguration instance
/// </summary>
/// <param name="httpConfig">The HttpConfiguration to apply this JsonApiHttpConfiguration to</param>
public void Apply(HttpConfiguration httpConfig)
{
httpConfig.Formatters.Clear();
httpConfig.Formatters.Add(_formatter);
httpConfig.Filters.Add(_fallbackDocumentBuilderAttribute);
httpConfig.Filters.Add(_jsonApiExceptionFilterAttribute);
// Web API routes
httpConfig.Routes.MapHttpRoute("ResourceCollection", "{resourceType}", new { controller = "Main" });
httpConfig.Routes.MapHttpRoute("Resource", "{resourceType}/{id}", new { controller = "Main" });
httpConfig.Routes.MapHttpRoute("RelatedResource", "{resourceType}/{id}/{relationshipName}", new { controller = "Main" });
}
}
}