Skip to content

Add-on for Func with ASP .NET Core specific features

License

Notifications You must be signed in to change notification settings

awsxdr/func-aspnetcore

Repository files navigation

Func-AspNet

License: MIT CI

Func.AspNet is an add-on for Func which simplifies the returning of Result objects from ASP .NET. The library handles the conversion from success and failure results to HTTP status codes as well as unwrapping the value (if any) contained in a success.

Getting Started

The following sections detail how to get started in Core and Framework versions of ASP. Full example projects are also available in the source.

ASP .NET Core

Configure Func.AspNet for use

Call the AddResultConversion extension method when configuring controllers:

services.AddControllers(config =>
{
  config.AddResultConversion();
});

Add error handling attributes as appropriate

Attributes can be added to controllers or to the errors themselves as shown below. Any unhandled error will produce a 500 response. When both attributes are present, the attribute on the controller will take precidence.

[HttpGet("")]
[OnFailure(typeof(PageNotFoundError), HttpStatusCode.NotFound, Message = "Page not found")]
[OnFailure(typeof(DocumentNotFoundError), HttpStatusCode.NotFound)]
public Result GetItem([FromQuery] string type)
{
  // Controller logic
}

-OR-

[ProducesStatusCode(HttpStatusCode.NotFound)]
public class NotFoundError : ResultError { }

Define message sources

Fixed error messages can be defined on the controller as shown above. If dynamic messages are required then a property or parameterless method should be defined on the error type.

[MessageTextSource(nameof(Message))]
public class DocumentNotFoundError : NotFoundError
{
  public Guid Id { get; } = Guid.NewGuid();
  public string Message => $"Could not find document {Id}";
}

Override success status code

By default, a success is returned with a 200 status code. If another status code is required, this can be achieved by using OnSuccessAttribute on the controller action:

[HttpPost("/")]
[OnSuccess(HttpStatusCode.Created)]
public Result CreateItem(ItemDetailsModel item)
{
  // Controller logic
}

ASP .NET

Configure Func.AspNet for use

Call the AddResultConversion extension method when configuring the HttpConfiguration object:

public static void Register(HttpConfiguration config)
{
    /*
      Other configuration...
    */

    config.AddResultConversion();
}

Add error handling attributes as appropriate

Attributes can be added to controllers or to the errors themselves as shown below. Any unhandled error will produce a 500 response. When both attributes are present, the attribute on the controller will take precidence.

[HttpGet, Route("")]
[OnFailure(typeof(PageNotFoundError), HttpStatusCode.NotFound, Message = "Page not found")]
[OnFailure(typeof(DocumentNotFoundError), HttpStatusCode.NotFound)]
public Result GetItem([FromQuery] string type)
{
  // Controller logic
}

-OR-

[ProducesStatusCode(HttpStatusCode.NotFound)]
public class NotFoundError : ResultError { }

Define message sources

Fixed error messages can be defined on the controller as shown above. If dynamic messages are required then a property or parameterless method should be defined on the error type.

[MessageTextSource(nameof(Message))]
public class DocumentNotFoundError : NotFoundError
{
  public Guid Id { get; } = Guid.NewGuid();
  public string Message => $"Could not find document {Id}";
}

Override success status code

By default, a success is returned with a 200 status code. If another status code is required, this can be achieved by using OnSuccessAttribute on the controller action:

[HttpPost, Route("/")]
[OnSuccess(HttpStatusCode.Created)]
public Result CreateItem(ItemDetailsModel item)
{
  // Controller logic
}

About

Add-on for Func with ASP .NET Core specific features

Resources

License

Stars

Watchers

Forks

Packages

No packages published