StrEnum.AspNetCore 2.0.0 The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved

.NET 5.0 .NET Core 3.1
Install-Package StrEnum.AspNetCore -Version 2.0.0
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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

StrEnum.AspNetCore

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

Supports ASP.NET Core 3.1–6.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 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 40 7/30/2022
1.0.0 93 5/19/2022