You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/usage/extensibility/controllers.md
+95-24Lines changed: 95 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,99 @@
1
1
# Controllers
2
2
3
-
You need to create controllers that inherit from `JsonApiController<TResource, TId>`
3
+
To expose API endpoints, ASP.NET controllers need to be defined.
4
+
5
+
_since v5_
6
+
7
+
Controllers are auto-generated (using [source generators](https://docs.microsoft.com/en-us/dotnet/csharp/roslyn-sdk/source-generators-overview)) when you add `[Resource]` on your model class:
8
+
9
+
```c#
10
+
[Resource] // Generates ArticlesController.g.cs
11
+
publicclassArticle : Identifiable<Guid>
12
+
{
13
+
// ...
14
+
}
15
+
```
16
+
17
+
## Resource Access Control
18
+
19
+
It is often desirable to limit which endpoints are exposed on your controller.
Instead of passing a set of endpoints, you can use `JsonApiEndpoints.Query` to generate all read-only endpoints or `JsonApiEndpoints.Command` for all write-only endpoints.
32
+
33
+
When an endpoint is blocked, an HTTP 403 Forbidden response is returned.
34
+
35
+
```http
36
+
DELETE http://localhost:14140/articles/1 HTTP/1.1
37
+
```
38
+
39
+
```json
40
+
{
41
+
"links": {
42
+
"self": "/articles"
43
+
},
44
+
"errors": [
45
+
{
46
+
"id": "dde7f219-2274-4473-97ef-baac3e7c1487",
47
+
"status": "403",
48
+
"title": "The requested endpoint is not accessible.",
49
+
"detail": "Endpoint '/articles/1' is not accessible for DELETE requests."
50
+
}
51
+
]
52
+
}
53
+
```
54
+
55
+
## Augmenting controllers
56
+
57
+
Auto-generated controllers can easily be augmented because they are partial classes. For example:
@@ -15,7 +108,7 @@ public class ArticlesController : JsonApiController<Article, Guid>
15
108
16
109
If you want to setup routes yourself, you can instead inherit from `BaseJsonApiController<TResource, TId>` and override its methods with your own `[HttpGet]`, `[HttpHead]`, `[HttpPost]`, `[HttpPatch]` and `[HttpDelete]` attributes added on them. Don't forget to add `[FromBody]` on parameters where needed.
17
110
18
-
## Resource Access Control
111
+
###Resource Access Control
19
112
20
113
It is often desirable to limit which routes are exposed on your controller.
21
114
@@ -37,25 +130,3 @@ public class ReportsController : JsonApiController<Report, int>
37
130
```
38
131
39
132
For more information about resource service injection, see [Replacing injected services](~/usage/extensibility/layer-overview.md#replacing-injected-services) and [Resource Services](~/usage/extensibility/services.md).
40
-
41
-
When a route is blocked, an HTTP 403 Forbidden response is returned.
42
-
43
-
```http
44
-
DELETE http://localhost:14140/people/1 HTTP/1.1
45
-
```
46
-
47
-
```json
48
-
{
49
-
"links": {
50
-
"self": "/api/v1/people"
51
-
},
52
-
"errors": [
53
-
{
54
-
"id": "dde7f219-2274-4473-97ef-baac3e7c1487",
55
-
"status": "403",
56
-
"title": "The requested endpoint is not accessible.",
57
-
"detail": "Endpoint '/people/1' is not accessible for DELETE requests."
Copy file name to clipboardExpand all lines: docs/usage/extensibility/services.md
+15-3Lines changed: 15 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,7 +1,8 @@
1
1
# Resource Services
2
2
3
3
The `IResourceService` acts as a service layer between the controller and the data access layer.
4
-
This allows you to customize it however you want. This is also a good place to implement custom business logic.
4
+
This allows you to customize it however you want. While this is still a potential place to implement custom business logic,
5
+
since v4, [Resource Definitions](~/usage/extensibility/resource-definitions.md) are more suitable for that.
5
6
6
7
## Supplementing Default Behavior
7
8
@@ -77,7 +78,7 @@ public class ProductService : IResourceService<Product, int>
77
78
78
79
## Limited Requirements
79
80
80
-
In some cases it may be necessary to only expose a few methods on a resource. For this reason, we have created a hierarchy of service interfaces that can be used to get the exact implementation you require.
81
+
In some cases it may be necessary to only expose a few actions on a resource. For this reason, we have created a hierarchy of service interfaces that can be used to get the exact implementation you require.
81
82
82
83
This interface hierarchy is defined by this tree structure.
83
84
@@ -152,7 +153,18 @@ public class Startup
152
153
}
153
154
```
154
155
155
-
Then in the controller, you should inherit from the JSON:API controller and pass the services into the named, optional base parameters:
156
+
Then on your model, pass in the set of endpoints to expose (the ones that you've registered services for):
157
+
158
+
```c#
159
+
[Resource(GenerateControllerEndpoints=
160
+
JsonApiEndpoints.Create|JsonApiEndpoints.Delete)]
161
+
publicclassArticle : Identifiable<int>
162
+
{
163
+
// ...
164
+
}
165
+
```
166
+
167
+
Alternatively, when using a hand-written controller, you should inherit from the JSON:API controller and pass the services into the named, optional base parameters:
Copy file name to clipboardExpand all lines: docs/usage/resource-graph.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -72,28 +72,28 @@ public void ConfigureServices(IServiceCollection services)
72
72
73
73
## Resource Name
74
74
75
-
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):
75
+
The public resource name is exposed through the `type` member in the JSON:API payload. This can be configured using the following approaches (in order of priority):
76
76
77
-
1. The `publicName` parameter when manually adding a resource to the graph
77
+
1. The `publicName` parameter when manually adding a resource to the graph.
Copy file name to clipboardExpand all lines: docs/usage/routing.md
+21-8Lines changed: 21 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,7 +6,7 @@ An endpoint URL provides access to a resource or a relationship. Resource endpoi
6
6
7
7
In the relationship endpoint "/articles/1/relationships/comments", "articles" is the left side of the relationship and "comments" the right side.
8
8
9
-
## Namespacing and Versioning URLs
9
+
## Namespacing and versioning of URLs
10
10
You can add a namespace to all URLs by specifying it in ConfigureServices.
11
11
12
12
```c#
@@ -18,15 +18,18 @@ public void ConfigureServices(IServiceCollection services)
18
18
19
19
Which results in URLs like: https://yourdomain.com/api/v1/people
20
20
21
-
## Default Routing Convention
21
+
## Default routing convention
22
22
23
-
The library will configure routes for all controllers in your project. By default, routes are camel-cased. This is based on the [recommendations](https://jsonapi.org/recommendations/) outlined in the JSON:API spec.
23
+
The library will configure routes for all auto-generated and hand-written controllers in your project. By default, routes are camel-cased. This is based on the [recommendations](https://jsonapi.org/recommendations/) outlined in the JSON:API spec.
@@ -73,7 +86,7 @@ public class OrderLineController : JsonApiController<OrderLine, int>
73
86
}
74
87
```
75
88
76
-
## Advanced Usage: Custom Routing Convention
89
+
## Advanced usage: Custom routing convention
77
90
78
91
It is possible to replace the built-in routing convention with a [custom routing convention](https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/application-model?view=aspnetcore-3.1#sample-custom-routing-convention) by registering an implementation of `IJsonApiRoutingConvention`.
0 commit comments