EfCore.GenericServices.AspNetCore 1.2.1

A support library to the EfCore.GenericServices library. It transfers IStatusGeneric errors into ASP.NET Core's ModelState

There is a newer version of this package available.
See the version list below for details.
Install-Package EfCore.GenericServices.AspNetCore -Version 1.2.1
dotnet add package EfCore.GenericServices.AspNetCore --version 1.2.1
<PackageReference Include="EfCore.GenericServices.AspNetCore" Version="1.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EfCore.GenericServices.AspNetCore --version 1.2.1
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

EfCore.GenericServices.AspNetCore

This library provides converters from EfCore.GenericService and EfCore.GenericBizRunner status results to into two ASP.NET Core formats.

  1. ASP.NET Core MVC or Razor pages - copy to ModelState.
  2. ASP.NET Core Web API - form correct HTTP response.

1. ASP.NET Core MVC or Razor pages - copy status to ModelState

The extention method CopyErrorsToModelState which copy IStatusGeneric errors into ASP.NET Core's ModelState.
Useful when working html/razor pages.

There are two forms of the method CopyErrorsToModelState - they are:

1a. CopyErrorsToModelState taking a dto

In ASP.NET Core MVC or Razor pages you need to return errors vai the ModelState, which has the property name and the error for that property. However it is important to NOT have a named property that doesn't appear in the class shown on the display, otherwise the error doesn't appear.

Because the Generic libraries can return errors with for properties not found in the display class there is a version of CopyErrorsToModelState that takes a parameter called displayDto and it will ensure the name is only set on properties that the displayDto has in it.

1b. CopyErrorsToModelState without a dto

If you want all the errors to have the property name left intact then there is another version that doesn't have a displayDto parameter.

2. ASP.NET Core Web API - forming the correct HTTP response

If you are using ASP.NET Core Web API you need the status codes of the Generic Libraries turned into the correct HTTP response. The CreateResponse static class contains a series of extension methods to do this for you.

NOTE: See this example Web API controller for examples of using the CreateResponse extension methods.

There are the following versions for both the GenericService and GenericBizRunner libraries:

  • IActionResult Response(this IStatusGeneric status) - this returns the status without any results.
  • IActionResult ResponseWithValidCode(this IStatusGeneric status, int validStatusCode) - this returns the status without any results, using the validStatusCode if the status has no errors.
  • ActionResult&lt;T&gt; Response&lt;T&gt;(this IStatusGeneric status, T results) - this returns the status with the results as a json object.
  • ActionResult&lt;T&gt; ResponseWithValidCode&lt;T&gt;(this IStatusGeneric status, T results, int validStatusCode) - this returns the status with the results as a json object, using the validStatusCode if the status has no errors.

Return formats

1. Success, no results

The HTTP status code defaults to 200, but you can change this by using the ResponseWithValidCode version of the methods. The json sent looks like this:

{
  "message": "Successfully deleted a Todo Item"
}
2. Success, with results - result is not null

The HTTP status code defaults to 200, but you can change this by using the ResponseWithValidCode version of the methods. The json sent looks like this:

{
  "message": "Success",
  "results": {
    "id": 1,
    "name": "Create ASP.NET Core API project",
    "difficulty": 1
  }
}
3. Success, with results where the results is null

The HTTP status code is 404 (NoFound). The json sent looks like this:

{
  "message": "The Todo Item was not found."
}
4. Error

The HTTP status code is 400 (BadRequest). The json sent looks like this:

{
    "": [
        "Global error message"
    ],    
    
    "MyPropery": [
        "The property is required",
        "Another error on the same property"
    ]
}

NOTE: This error format is the one that ASP.NET Core WebAPI when it is set up to validate data on input.

MIT licence

EfCore.GenericServices.AspNetCore

This library provides converters from EfCore.GenericService and EfCore.GenericBizRunner status results to into two ASP.NET Core formats.

  1. ASP.NET Core MVC or Razor pages - copy to ModelState.
  2. ASP.NET Core Web API - form correct HTTP response.

1. ASP.NET Core MVC or Razor pages - copy status to ModelState

The extention method CopyErrorsToModelState which copy IStatusGeneric errors into ASP.NET Core's ModelState.
Useful when working html/razor pages.

There are two forms of the method CopyErrorsToModelState - they are:

1a. CopyErrorsToModelState taking a dto

In ASP.NET Core MVC or Razor pages you need to return errors vai the ModelState, which has the property name and the error for that property. However it is important to NOT have a named property that doesn't appear in the class shown on the display, otherwise the error doesn't appear.

Because the Generic libraries can return errors with for properties not found in the display class there is a version of CopyErrorsToModelState that takes a parameter called displayDto and it will ensure the name is only set on properties that the displayDto has in it.

1b. CopyErrorsToModelState without a dto

If you want all the errors to have the property name left intact then there is another version that doesn't have a displayDto parameter.

2. ASP.NET Core Web API - forming the correct HTTP response

If you are using ASP.NET Core Web API you need the status codes of the Generic Libraries turned into the correct HTTP response. The CreateResponse static class contains a series of extension methods to do this for you.

NOTE: See this example Web API controller for examples of using the CreateResponse extension methods.

There are the following versions for both the GenericService and GenericBizRunner libraries:

  • IActionResult Response(this IStatusGeneric status) - this returns the status without any results.
  • IActionResult ResponseWithValidCode(this IStatusGeneric status, int validStatusCode) - this returns the status without any results, using the validStatusCode if the status has no errors.
  • ActionResult&lt;T&gt; Response&lt;T&gt;(this IStatusGeneric status, T results) - this returns the status with the results as a json object.
  • ActionResult&lt;T&gt; ResponseWithValidCode&lt;T&gt;(this IStatusGeneric status, T results, int validStatusCode) - this returns the status with the results as a json object, using the validStatusCode if the status has no errors.

Return formats

1. Success, no results

The HTTP status code defaults to 200, but you can change this by using the ResponseWithValidCode version of the methods. The json sent looks like this:

{
  "message": "Successfully deleted a Todo Item"
}
2. Success, with results - result is not null

The HTTP status code defaults to 200, but you can change this by using the ResponseWithValidCode version of the methods. The json sent looks like this:

{
  "message": "Success",
  "results": {
    "id": 1,
    "name": "Create ASP.NET Core API project",
    "difficulty": 1
  }
}
3. Success, with results where the results is null

The HTTP status code is 404 (NoFound). The json sent looks like this:

{
  "message": "The Todo Item was not found."
}
4. Error

The HTTP status code is 400 (BadRequest). The json sent looks like this:

{
    "": [
        "Global error message"
    ],    
    
    "MyPropery": [
        "The property is required",
        "Another error on the same property"
    ]
}

NOTE: This error format is the one that ASP.NET Core WebAPI when it is set up to validate data on input.

MIT licence

Release Notes

- New feature: Added `ResponseWithValidCode` methods to allow the StatusCode to be set for the success path - useful for things like 'POST' where you might want to return a status code of 201 instead of 200
     - Updated to newest versions of GenericServices and GenericBizRunner

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on EfCore.GenericServices.AspNetCore:

Repository Stars
JonPSmith/PermissionAccessControl2
Version 2 of example application to go with articles on feature and data authorization

Version History

Version Downloads Last updated
4.1.1 89 6/19/2020
4.1.0 997 3/18/2020
4.0.0 1,823 10/14/2019
4.0.0-preview001 121 2/28/2020
3.0.2 11,311 9/5/2018
3.0.1 253 8/31/2018
3.0.0 261 8/22/2018
2.0.0 309 8/14/2018
1.2.1 360 7/30/2018
1.2.0 280 7/25/2018
1.1.0 1,222 6/7/2018
1.0.0 384 4/12/2018
Show less