Class JsonApiController<TResource, TId>
- Namespace
- JsonApiDotNetCore.Controllers
- Assembly
- JsonApiDotNetCore.dll
The base class to derive resource-specific controllers from. This class delegates all work to BaseJsonApiController<TResource, TId> but adds attributes for routing templates. If you want to provide routing templates yourself, you should derive from BaseJsonApiController directly.
public abstract class JsonApiController<TResource, TId> : BaseJsonApiController<TResource, TId> where TResource : class, IIdentifiable<TId>
Type Parameters
TResource
The resource type.
TId
The resource identifier type.
- Inheritance
-
BaseJsonApiController<TResource, TId>JsonApiController<TResource, TId>
- Derived
- Inherited Members
Constructors
JsonApiController(IJsonApiOptions, IResourceGraph, ILoggerFactory, IGetAllService<TResource, TId>?, IGetByIdService<TResource, TId>?, IGetSecondaryService<TResource, TId>?, IGetRelationshipService<TResource, TId>?, ICreateService<TResource, TId>?, IAddToRelationshipService<TResource, TId>?, IUpdateService<TResource, TId>?, ISetRelationshipService<TResource, TId>?, IDeleteService<TResource, TId>?, IRemoveFromRelationshipService<TResource, TId>?)
Creates an instance from separate services for the various individual read and write methods.
protected JsonApiController(IJsonApiOptions options, IResourceGraph resourceGraph, ILoggerFactory loggerFactory, IGetAllService<TResource, TId>? getAll = null, IGetByIdService<TResource, TId>? getById = null, IGetSecondaryService<TResource, TId>? getSecondary = null, IGetRelationshipService<TResource, TId>? getRelationship = null, ICreateService<TResource, TId>? create = null, IAddToRelationshipService<TResource, TId>? addToRelationship = null, IUpdateService<TResource, TId>? update = null, ISetRelationshipService<TResource, TId>? setRelationship = null, IDeleteService<TResource, TId>? delete = null, IRemoveFromRelationshipService<TResource, TId>? removeFromRelationship = null)
Parameters
options
IJsonApiOptionsresourceGraph
IResourceGraphloggerFactory
ILoggerFactorygetAll
IGetAllService<TResource, TId>getById
IGetByIdService<TResource, TId>getSecondary
IGetSecondaryService<TResource, TId>getRelationship
IGetRelationshipService<TResource, TId>create
ICreateService<TResource, TId>addToRelationship
IAddToRelationshipService<TResource, TId>update
IUpdateService<TResource, TId>setRelationship
ISetRelationshipService<TResource, TId>delete
IDeleteService<TResource, TId>removeFromRelationship
IRemoveFromRelationshipService<TResource, TId>
JsonApiController(IJsonApiOptions, IResourceGraph, ILoggerFactory, IResourceService<TResource, TId>)
Creates an instance from a read/write service.
protected JsonApiController(IJsonApiOptions options, IResourceGraph resourceGraph, ILoggerFactory loggerFactory, IResourceService<TResource, TId> resourceService)
Parameters
options
IJsonApiOptionsresourceGraph
IResourceGraphloggerFactory
ILoggerFactoryresourceService
IResourceService<TResource, TId>
Methods
DeleteAsync(TId, CancellationToken)
Deletes an existing resource. Example:
DELETE /articles/1 HTTP/1.1
[HttpDelete("{id}")]
public override Task<IActionResult> DeleteAsync(TId id, CancellationToken cancellationToken)
Parameters
id
TIdcancellationToken
CancellationToken
Returns
DeleteRelationshipAsync(TId, string, ISet<IIdentifiable>, CancellationToken)
Removes resources from a to-many relationship. Example:
DELETE /articles/1/relationships/revisions HTTP/1.1
[HttpDelete("{id}/relationships/{relationshipName}")]
public override Task<IActionResult> DeleteRelationshipAsync(TId id, string relationshipName, ISet<IIdentifiable> rightResourceIds, CancellationToken cancellationToken)
Parameters
id
TIdIdentifies the left side of the relationship.
relationshipName
stringThe relationship to remove resources from.
rightResourceIds
ISet<IIdentifiable>The set of resources to remove from the relationship.
cancellationToken
CancellationTokenPropagates notification that request handling should be canceled.
Returns
GetAsync(CancellationToken)
Gets a collection of primary resources. Example:
GET /articles HTTP/1.1
[HttpGet]
[HttpHead]
public override Task<IActionResult> GetAsync(CancellationToken cancellationToken)
Parameters
cancellationToken
CancellationToken
Returns
GetAsync(TId, CancellationToken)
Gets a single primary resource by ID. Example:
GET /articles/1 HTTP/1.1
[HttpGet("{id}")]
[HttpHead("{id}")]
public override Task<IActionResult> GetAsync(TId id, CancellationToken cancellationToken)
Parameters
id
TIdcancellationToken
CancellationToken
Returns
GetRelationshipAsync(TId, string, CancellationToken)
Gets a relationship value, which can be a null
, a single object or a collection. Example:
GET /articles/1/relationships/author HTTP/1.1
Example:
GET /articles/1/relationships/revisions HTTP/1.1
[HttpGet("{id}/relationships/{relationshipName}")]
[HttpHead("{id}/relationships/{relationshipName}")]
public override Task<IActionResult> GetRelationshipAsync(TId id, string relationshipName, CancellationToken cancellationToken)
Parameters
id
TIdrelationshipName
stringcancellationToken
CancellationToken
Returns
GetSecondaryAsync(TId, string, CancellationToken)
Gets a secondary resource or collection of secondary resources. Example:
GET /articles/1/author HTTP/1.1
Example:
GET /articles/1/revisions HTTP/1.1
[HttpGet("{id}/{relationshipName}")]
[HttpHead("{id}/{relationshipName}")]
public override Task<IActionResult> GetSecondaryAsync(TId id, string relationshipName, CancellationToken cancellationToken)
Parameters
id
TIdrelationshipName
stringcancellationToken
CancellationToken
Returns
PatchAsync(TId, TResource, CancellationToken)
Updates the attributes and/or relationships of an existing resource. Only the values of sent attributes are replaced. And only the values of sent relationships are replaced. Example:
PATCH /articles/1 HTTP/1.1
[HttpPatch("{id}")]
public override Task<IActionResult> PatchAsync(TId id, TResource resource, CancellationToken cancellationToken)
Parameters
id
TIdresource
TResourcecancellationToken
CancellationToken
Returns
PatchRelationshipAsync(TId, string, object?, CancellationToken)
Performs a complete replacement of a relationship on an existing resource. Example:
PATCH /articles/1/relationships/author HTTP/1.1
Example:
PATCH /articles/1/relationships/revisions HTTP/1.1
[HttpPatch("{id}/relationships/{relationshipName}")]
public override Task<IActionResult> PatchRelationshipAsync(TId id, string relationshipName, object? rightValue, CancellationToken cancellationToken)
Parameters
id
TIdIdentifies the left side of the relationship.
relationshipName
stringThe relationship for which to perform a complete replacement.
rightValue
objectThe resource or set of resources to assign to the relationship.
cancellationToken
CancellationTokenPropagates notification that request handling should be canceled.
Returns
PostAsync(TResource, CancellationToken)
Creates a new resource with attributes, relationships or both. Example:
POST /articles HTTP/1.1
[HttpPost]
public override Task<IActionResult> PostAsync(TResource resource, CancellationToken cancellationToken)
Parameters
resource
TResourcecancellationToken
CancellationToken
Returns
PostRelationshipAsync(TId, string, ISet<IIdentifiable>, CancellationToken)
Adds resources to a to-many relationship. Example:
POST /articles/1/revisions HTTP/1.1
[HttpPost("{id}/relationships/{relationshipName}")]
public override Task<IActionResult> PostRelationshipAsync(TId id, string relationshipName, ISet<IIdentifiable> rightResourceIds, CancellationToken cancellationToken)
Parameters
id
TIdIdentifies the left side of the relationship.
relationshipName
stringThe relationship to add resources to.
rightResourceIds
ISet<IIdentifiable>The set of resources to add to the relationship.
cancellationToken
CancellationTokenPropagates notification that request handling should be canceled.