StrEnum.AspNetCore 2.0.0

.NET 5.0 .NET Core 3.1
NuGet\Install-Package StrEnum.AspNetCore -Version 2.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.
dotnet add package StrEnum.AspNetCore --version 2.0.0
<PackageReference Include="StrEnum.AspNetCore" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add StrEnum.AspNetCore --version 2.0.0
#r "nuget: StrEnum.AspNetCore, 2.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. 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=2.0.0

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


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

Supports ASP.NET Core 3.1–6.0


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

dotnet add package StrEnum.AspNetCore


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

var builder = WebApplication.CreateBuilder(args);


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)

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:

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:

public ActionResult<ResponseWithStrEnum> GetFromRoute(Sport sport) {...}

To a query string parameter:

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

And to an array of query string parameters:

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


Copyright © 2022 Dmitry Khmara.

StrEnum is licensed under the MIT license.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows
.NET Core netcoreapp3.1
Compatible target framework(s)
Additional computed target framework(s)
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
2.0.0 71 7/30/2022
1.0.0 109 5/19/2022