StrEnum.AspNetCore
3.1.0
Prefix Reserved
dotnet add package StrEnum.AspNetCore --version 3.1.0
NuGet\Install-Package StrEnum.AspNetCore -Version 3.1.0
<PackageReference Include="StrEnum.AspNetCore" Version="3.1.0" />
paket add StrEnum.AspNetCore --version 3.1.0
#r "nuget: StrEnum.AspNetCore, 3.1.0"
// Install StrEnum.AspNetCore as a Cake Addin #addin nuget:?package=StrEnum.AspNetCore&version=3.1.0 // Install StrEnum.AspNetCore as a Cake Tool #tool nuget:?package=StrEnum.AspNetCore&version=3.1.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 | Versions 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. |
-
.NETCoreApp 3.1
- StrEnum (>= 2.0.0 && < 3.0.0)
- StrEnum.System.Text.Json (>= 2.1.1 && < 3.0.0)
-
net6.0
- StrEnum (>= 2.0.0 && < 3.0.0)
- StrEnum.System.Text.Json (>= 2.1.1 && < 3.0.0)
-
net7.0
- StrEnum (>= 2.0.0 && < 3.0.0)
- StrEnum.System.Text.Json (>= 2.1.1 && < 3.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.