StrEnum.AspNetCore 3.0.0

Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package StrEnum.AspNetCore --version 3.0.0                
NuGet\Install-Package StrEnum.AspNetCore -Version 3.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="StrEnum.AspNetCore" Version="3.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add StrEnum.AspNetCore --version 3.0.0                
#r "nuget: StrEnum.AspNetCore, 3.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install StrEnum.AspNetCore as a Cake Addin
#addin nuget:?package=StrEnum.AspNetCore&version=3.0.0

// Install StrEnum.AspNetCore as a Cake Tool
#tool nuget:?package=StrEnum.AspNetCore&version=3.0.0                

StrEnum.AspNetCore

Allows to use StrEnum string enums with ASP.NET Core.

Supports ASP.NET Core 6 and 7. ASP.NET Core supported in v2.0.0.

Installation

You can install StrEnum.AspNetCore using the .NET CLI:

dotnet add package StrEnum.AspNetCore

Usage

If you're using the new ASP.NET Core 6WebApplicationBuilder, add the call to AddStringEnums() into your Program.cs:

var builder = WebApplication.CreateBuilder(args);

builder.Services
    .AddControllers()
    .AddStringEnums();

If you're using the ASP.NET Core 3.1/5 IWebHostBuilder, call AddStringEnums() in the ConfigureServices method of your Startup.cs:

public void ConfigureServices(IServiceCollection services)
{
     services
         .AddControllers()
         .AddStringEnums();
}

All set. Let's now create a string enum and a model that contains it:

public class Sport : StringEnum<Sport>
{
    public static Sport TrailRunning = Define("TRAIL_RUNNING");
    public static Sport RoadCycling = Define("ROAD_CYCLING");
}

public class Race
{
    public string Name { get; set; }
    public Sport Sport { get; set; }
}

You can bind string enums to the request body and return them in the response. In your controller, add the following:

[HttpPost]
public ActionResult<Race> BodyPost([FromBody] Race race) // race.Sport is correctly deserialized
{
    return Ok(race); // race.Sport is serialized back
}

You can also bind string enums to a URL:

[HttpGet]
[Route("{sport}")]
public ActionResult<ResponseWithStrEnum> GetFromRoute(Sport sport) {...}

To a query string parameter:

[HttpGet]
[Route("get")]
public ActionResult<ResponseWithStrEnum> GetFromQuery([FromQuery]Sport sport) {...}
// `get?sport=trail_running` binds to Sport.TrailRunning

And to an array of query string parameters:

[HttpGet]
[Route("get")]
public ActionResult<ResponseWithStrEnum> GetFromQuery([FromQuery] Sport[] sports) {...}
// `get?sports=trail_running&sports=road_cycling` binds to [Sport.TrailRunning, Sport.RoadCycling]

License

Copyright © 2022 Dmitry Khmara.

StrEnum is licensed under the MIT license.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.1.0 557 6/11/2023
3.0.0 164 6/11/2023
2.0.0 438 7/30/2022
1.0.0 423 5/19/2022