Skip to content

Commit 21ed990

Browse files
committed
fix: docs, publicName
1 parent 526b0c6 commit 21ed990

File tree

5 files changed

+89
-17
lines changed

5 files changed

+89
-17
lines changed
Lines changed: 76 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,86 @@
11
# Middleware
2-
Add the following to your Startup.ConfigureServices method. Replace AppDbContext with your DbContext.
32

4-
```c3
3+
The following is the default configuration of JsonApiDotNetCore:
4+
1. Call one of the `AddJsonApi( ... )` overloads in the ` Startup.ConfigureServices` method. In this example uses a `DbContext` to build the resource graph
5+
6+
```c#
57
services.AddJsonApi<AppDbContext>();
68
```
79

8-
Add the middleware to the Startup.Configure method.
10+
2. In the Startup.Configure method, configure your application to use routing, to add the JsonApiMiddleware and to configure endpoint routing.
911

10-
```c3
12+
```c#
1113
app.UseRouting();
1214
app.UseJsonApi();
1315
app.UseEndpoints(endpoints => endpoints.MapControllers());
1416
```
17+
18+
The following middleware components, in respective order, are registered:
19+
20+
21+
22+
Filters:
23+
- `IJsonApiExceptionFilter`
24+
- `IJsonApiTypeMatchFilter`
25+
- `IQueryStringActionFilter`
26+
- `IConvertEmptyActionResultFilter`
27+
28+
Formatters:
29+
- `IJsonApiInputFormatter`
30+
- `IJsonApiOutputFormatter`
31+
32+
Routing convention:
33+
- `IJsonApiRoutingConvention`
34+
35+
Middleware:
36+
- `JsonApiMiddleware`
37+
38+
All of these components (except for `JsonApiMiddleware`) can be customized by registering your own implementation of these services. For example:
39+
40+
```c#
41+
services.AddSingleton<IJsonApiExceptionFilter, MyCustomExceptionFilter>();
42+
```
43+
44+
It is also possible to directly access the .NET Core `MvcOptions` object and have full controll over which components are registered.
45+
46+
## Configuring MvcOptions
47+
48+
JsonApiDotNetCore internally configures `MvcOptions` when calling `AddJsonApi( ... )`. However, it is still possible to register a custom configuration callback. To achieve this it is recommended to register this callback *after* the `AddJsonApi( ... )` call. It is also possible to do it earlier, but your configuration might be overridden by the JsonApiDotNetCore configuration.
49+
50+
The following example replaces all internally registered filters by retrieving a custom filter from the DI container.
51+
```c#
52+
public class Startup
53+
{
54+
private Action<MvcOptions> _postConfigureMvcOptions;
55+
56+
public void ConfigureServices(IServiceCollection services)
57+
{
58+
...
59+
60+
var builder = services.AddMvcCore();
61+
services.AddJsonApi<AppDbContext>( ... , mvcBuilder: builder);
62+
mvcCoreBuilder.AddMvcOptions(x =>
63+
{
64+
// execute the mvc configuration callback after the JsonApiDotNetCore callback as been executed.
65+
_postConfigureMvcOptions?.Invoke(x);
66+
});
67+
68+
...
69+
}
70+
71+
public void Configure(IApplicationBuilder app, IWebHostEnvironment environment)
72+
{
73+
74+
...
75+
76+
// Using a callback, we can defer to later (when service collection has become available).
77+
_postConfigureMvcOptions = mvcOptions =>
78+
{
79+
mvcOptions.Filters.Clear();
80+
mvcOptions.Filters.Insert(0, app.ApplicationServices.GetService<CustomFilter>());
81+
};
82+
83+
...
84+
}
85+
}
86+
```

docs/usage/options.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ options.SerializerSettings.Formatting = Formatting.Indented;
9292

9393
Because we copy resource properties into an intermediate object before serialization, Newtonsoft.Json annotations on properties are ignored.
9494

95+
9596
## Enable ModelState Validation
9697

9798
If you would like to use ASP.NET Core ModelState validation into your controllers when creating / updating resources, set `ValidateModelState = true`. By default, no model validation is performed.

docs/usage/resource-graph.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ public void ConfigureServices(IServiceCollection services)
8181

8282
The public resource name is exposed through the `type` member in the json:api payload. This can be configured by the following approaches (in order of priority):
8383

84-
1. The `publicResourceName` option when manually adding a resource to the graph
84+
1. The `publicName` option when manually adding a resource to the graph
8585
```c#
8686
services.AddJsonApi(resources: builder =>
8787
{
88-
builder.AddResource<Person>(publicResourceName: "people");
88+
builder.AddResource<Person>(publicName: "people");
8989
});
9090
```
9191

docs/usage/routing.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,12 @@ GET /myResources HTTP/1.1
3838
```
3939

4040
## Customized the Routing Convention
41-
It is possible to fully customize routing behaviour by registering a `IJsonApiRoutingConvention` implementation **before** calling `AddJsonApi( ... )`.
41+
It is possible to fully customize routing behaviour by registering a `IJsonApiRoutingConvention` implementation.
4242
```c#
4343
// Startup.cs
4444
public void ConfigureServices(IServiceCollection services)
4545
{
4646
services.AddSingleton<IJsonApiConvention, CustomRoutingConvention>();
47-
services.AddJsonApi( /* ... */ );
4847
}
4948
```
5049

src/JsonApiDotNetCore/Configuration/ResourceGraphBuilder.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,25 @@ private void SetResourceLinksOptions(ResourceContext resourceContext)
4040
}
4141

4242
/// <inheritdoc />
43-
public ResourceGraphBuilder Add<TResource>(string publicResourceName = null) where TResource : class, IIdentifiable<int>
44-
=> Add<TResource, int>(publicResourceName);
43+
public ResourceGraphBuilder Add<TResource>(string publicName = null) where TResource : class, IIdentifiable<int>
44+
=> Add<TResource, int>(publicName);
4545

4646
/// <inheritdoc />
47-
public ResourceGraphBuilder Add<TResource, TId>(string publicResourceName = null) where TResource : class, IIdentifiable<TId>
48-
=> Add(typeof(TResource), typeof(TId), publicResourceName);
47+
public ResourceGraphBuilder Add<TResource, TId>(string publicName = null) where TResource : class, IIdentifiable<TId>
48+
=> Add(typeof(TResource), typeof(TId), publicName);
4949

5050
/// <inheritdoc />
51-
public ResourceGraphBuilder Add(Type resourceType, Type idType = null, string publicResourceName = null)
51+
public ResourceGraphBuilder Add(Type resourceType, Type idType = null, string publicName = null)
5252
{
5353
if (resourceType == null) throw new ArgumentNullException(nameof(resourceType));
5454

5555
if (_resources.All(e => e.ResourceType != resourceType))
5656
{
5757
if (TypeHelper.IsOrImplementsInterface(resourceType, typeof(IIdentifiable)))
5858
{
59-
publicResourceName ??= FormatResourceName(resourceType);
59+
publicName ??= FormatResourceName(resourceType);
6060
idType ??= TypeLocator.GetIdType(resourceType);
61-
var resourceContext = CreateResourceContext(publicResourceName, resourceType, idType);
61+
var resourceContext = CreateResourceContext(publicName, resourceType, idType);
6262
_resources.Add(resourceContext);
6363
}
6464
else
@@ -70,9 +70,9 @@ public ResourceGraphBuilder Add(Type resourceType, Type idType = null, string pu
7070
return this;
7171
}
7272

73-
private ResourceContext CreateResourceContext(string publicResourceName, Type resourceType, Type idType) => new ResourceContext
73+
private ResourceContext CreateResourceContext(string publicName, Type resourceType, Type idType) => new ResourceContext
7474
{
75-
ResourceName = publicResourceName,
75+
ResourceName = publicName,
7676
ResourceType = resourceType,
7777
IdentityType = idType,
7878
Attributes = GetAttributes(resourceType),

0 commit comments

Comments
 (0)