Table of Contents

Class BaseJsonApiController<TResource, TId>

Namespace
JsonApiDotNetCore.Controllers
Assembly
JsonApiDotNetCore.dll

Implements the foundational ASP.NET controller layer in the JsonApiDotNetCore architecture that delegates to a Resource Service.

public abstract class BaseJsonApiController<TResource, TId> : CoreJsonApiController where TResource : class, IIdentifiable<TId>

Type Parameters

TResource

The resource type.

TId

The resource identifier type.

Inheritance
BaseJsonApiController<TResource, TId>
Derived
Inherited Members

Constructors

BaseJsonApiController(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 BaseJsonApiController(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 IJsonApiOptions
resourceGraph IResourceGraph
loggerFactory ILoggerFactory
getAll 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>

BaseJsonApiController(IJsonApiOptions, IResourceGraph, ILoggerFactory, IResourceQueryService<TResource, TId>?, IResourceCommandService<TResource, TId>?)

Creates an instance from separate services for reading and writing.

protected BaseJsonApiController(IJsonApiOptions options, IResourceGraph resourceGraph, ILoggerFactory loggerFactory, IResourceQueryService<TResource, TId>? queryService = null, IResourceCommandService<TResource, TId>? commandService = null)

Parameters

options IJsonApiOptions
resourceGraph IResourceGraph
loggerFactory ILoggerFactory
queryService IResourceQueryService<TResource, TId>
commandService IResourceCommandService<TResource, TId>

BaseJsonApiController(IJsonApiOptions, IResourceGraph, ILoggerFactory, IResourceService<TResource, TId>)

Creates an instance from a read/write service.

protected BaseJsonApiController(IJsonApiOptions options, IResourceGraph resourceGraph, ILoggerFactory loggerFactory, IResourceService<TResource, TId> resourceService)

Parameters

options IJsonApiOptions
resourceGraph IResourceGraph
loggerFactory ILoggerFactory
resourceService IResourceService<TResource, TId>

Methods

DeleteAsync(TId, CancellationToken)

Deletes an existing resource. Example:

DELETE /articles/1 HTTP/1.1
public virtual Task<IActionResult> DeleteAsync(TId id, CancellationToken cancellationToken)

Parameters

id TId
cancellationToken CancellationToken

Returns

Task<IActionResult>

DeleteRelationshipAsync(TId, string, ISet<IIdentifiable>, CancellationToken)

Removes resources from a to-many relationship. Example:

DELETE /articles/1/relationships/revisions HTTP/1.1
public virtual Task<IActionResult> DeleteRelationshipAsync(TId id, string relationshipName, ISet<IIdentifiable> rightResourceIds, CancellationToken cancellationToken)

Parameters

id TId

Identifies the left side of the relationship.

relationshipName string

The relationship to remove resources from.

rightResourceIds ISet<IIdentifiable>

The set of resources to remove from the relationship.

cancellationToken CancellationToken

Propagates notification that request handling should be canceled.

Returns

Task<IActionResult>

GetAsync(CancellationToken)

Gets a collection of primary resources. Example:

GET /articles HTTP/1.1
public virtual Task<IActionResult> GetAsync(CancellationToken cancellationToken)

Parameters

cancellationToken CancellationToken

Returns

Task<IActionResult>

GetAsync(TId, CancellationToken)

Gets a single primary resource by ID. Example:

GET /articles/1 HTTP/1.1
public virtual Task<IActionResult> GetAsync(TId id, CancellationToken cancellationToken)

Parameters

id TId
cancellationToken CancellationToken

Returns

Task<IActionResult>

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
public virtual Task<IActionResult> GetRelationshipAsync(TId id, string relationshipName, CancellationToken cancellationToken)

Parameters

id TId
relationshipName string
cancellationToken CancellationToken

Returns

Task<IActionResult>

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
public virtual Task<IActionResult> GetSecondaryAsync(TId id, string relationshipName, CancellationToken cancellationToken)

Parameters

id TId
relationshipName string
cancellationToken CancellationToken

Returns

Task<IActionResult>

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
public virtual Task<IActionResult> PatchAsync(TId id, TResource resource, CancellationToken cancellationToken)

Parameters

id TId
resource TResource
cancellationToken CancellationToken

Returns

Task<IActionResult>

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
public virtual Task<IActionResult> PatchRelationshipAsync(TId id, string relationshipName, object? rightValue, CancellationToken cancellationToken)

Parameters

id TId

Identifies the left side of the relationship.

relationshipName string

The relationship for which to perform a complete replacement.

rightValue object

The resource or set of resources to assign to the relationship.

cancellationToken CancellationToken

Propagates notification that request handling should be canceled.

Returns

Task<IActionResult>

PostAsync(TResource, CancellationToken)

Creates a new resource with attributes, relationships or both. Example:

POST /articles HTTP/1.1
public virtual Task<IActionResult> PostAsync(TResource resource, CancellationToken cancellationToken)

Parameters

resource TResource
cancellationToken CancellationToken

Returns

Task<IActionResult>

PostRelationshipAsync(TId, string, ISet<IIdentifiable>, CancellationToken)

Adds resources to a to-many relationship. Example:

POST /articles/1/revisions HTTP/1.1
public virtual Task<IActionResult> PostRelationshipAsync(TId id, string relationshipName, ISet<IIdentifiable> rightResourceIds, CancellationToken cancellationToken)

Parameters

id TId

Identifies the left side of the relationship.

relationshipName string

The relationship to add resources to.

rightResourceIds ISet<IIdentifiable>

The set of resources to add to the relationship.

cancellationToken CancellationToken

Propagates notification that request handling should be canceled.

Returns

Task<IActionResult>