Class ResourceNotFoundException
Exception thrown when a requested resource cannot be found.
public sealed class ResourceNotFoundException : ResourceException, ISerializable
- Inheritance
-
ResourceNotFoundException
- Implements
- Inherited Members
Examples
var user = await repository.FindByIdAsync(userId, cancellationToken)
?? throw new ResourceNotFoundException("User", userId.ToString());
Remarks
This exception indicates that a specific resource (entity, record, or object) does not exist or could not be located. It maps to HTTP status code 404 (Not Found).
Use this exception when:
- Looking up an entity by ID that doesn't exist
- Attempting to access a resource that has been deleted
- Querying for a record that doesn't match any criteria
Constructors
ResourceNotFoundException()
Initializes a new instance of the ResourceNotFoundException class.
public ResourceNotFoundException()
ResourceNotFoundException(string)
Initializes a new instance of the ResourceNotFoundException class with a specified error message.
public ResourceNotFoundException(string message)
Parameters
messagestringThe error message that explains the reason for the exception.
ResourceNotFoundException(string, Exception?)
Initializes a new instance of the ResourceNotFoundException class with a message and inner exception.
public ResourceNotFoundException(string message, Exception? innerException)
Parameters
messagestringThe error message that explains the reason for the exception.
innerExceptionExceptionThe exception that is the cause of the current exception.
ResourceNotFoundException(string, string?)
Initializes a new instance of the ResourceNotFoundException class with resource information.
public ResourceNotFoundException(string resource, string? resourceId)
Parameters
resourcestringThe type or name of the resource that was not found.
resourceIdstringThe optional identifier of the resource that was not found.
Methods
ForEntity<TEntity>(object)
Creates a ResourceNotFoundException for a specific entity type and ID.
public static ResourceNotFoundException ForEntity<TEntity>(object id)
Parameters
idobjectThe identifier of the entity.
Returns
- ResourceNotFoundException
A new ResourceNotFoundException instance.
Type Parameters
TEntityThe type of the entity.