|
| 1 | +# StrEnum.AspNetCore |
| 2 | + |
| 3 | +Allows to use [StrEnum](https://github.com/StrEnum/StrEnum/) string enums with ASP.NET Core. |
| 4 | + |
| 5 | +Supports ASP.NET Core 3.1–6.0 |
| 6 | + |
| 7 | +## Installation |
| 8 | + |
| 9 | +You can install [StrEnum.AspNetCore](https://www.nuget.org/packages/StrEnum.AspNetCore/) using the .NET CLI: |
| 10 | + |
| 11 | +``` |
| 12 | +dotnet add package StrEnum.AspNetCore |
| 13 | +``` |
| 14 | + |
| 15 | +## Usage |
| 16 | + |
| 17 | +If you're using the new ASP.NET Core 6`WebApplicationBuilder`, add the call to `AddStringEnums()` into your `Program.cs`: |
| 18 | + |
| 19 | +```csharp |
| 20 | +var builder = WebApplication.CreateBuilder(args); |
| 21 | + |
| 22 | +builder.Services |
| 23 | + .AddControllers() |
| 24 | + .AddStringEnums(); |
| 25 | +``` |
| 26 | + |
| 27 | +If you're using the ASP.NET Core 3.1/5 `IWebHostBuilder`, call `AddStringEnums()` in the `ConfigureServices` method of your `Startup.cs`: |
| 28 | + |
| 29 | +```csharp |
| 30 | +public void ConfigureServices(IServiceCollection services) |
| 31 | +{ |
| 32 | + services |
| 33 | + .AddControllers() |
| 34 | + .AddStringEnums(); |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +All set. Let's now create a string enum and a model that contains it: |
| 39 | + |
| 40 | +```csharp |
| 41 | +public class Sport : StringEnum<Sport> |
| 42 | +{ |
| 43 | + public static Sport TrailRunning = Define("TRAIL_RUNNING"); |
| 44 | + public static Sport RoadCycling = Define("ROAD_CYCLING"); |
| 45 | +} |
| 46 | + |
| 47 | +public class Race |
| 48 | +{ |
| 49 | + public string Name { get; set; } |
| 50 | + public Sport Sport { get; set; } |
| 51 | +} |
| 52 | +``` |
| 53 | + |
| 54 | +You can bind string enums to the request body and return them in the response. In your controller, add the following: |
| 55 | + |
| 56 | +```csharp |
| 57 | +[HttpPost] |
| 58 | +public ActionResult<Race> BodyPost([FromBody] Race race) // race.Sport is correctly deserialized |
| 59 | +{ |
| 60 | + return Ok(race); // race.Sport is serialized back |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +You can also bind string enums to a URL: |
| 65 | + |
| 66 | +```csharp |
| 67 | +[HttpGet] |
| 68 | +[Route("{sport}")] |
| 69 | +public ActionResult<ResponseWithStrEnum> GetFromRoute(Sport sport) {...} |
| 70 | +``` |
| 71 | + |
| 72 | +To a query string parameter: |
| 73 | + |
| 74 | +```csharp |
| 75 | +[HttpGet] |
| 76 | +[Route("get")] |
| 77 | +public ActionResult<ResponseWithStrEnum> GetFromQuery([FromQuery]Sport sport) {...} |
| 78 | +// `get?sport=trail_running` binds to Sport.TrailRunning |
| 79 | +``` |
| 80 | + |
| 81 | +And to an array of query string parameters: |
| 82 | + |
| 83 | +```csharp |
| 84 | +[HttpGet] |
| 85 | +[Route("get")] |
| 86 | +public ActionResult<ResponseWithStrEnum> GetFromQuery([FromQuery] Sport[] sports) {...} |
| 87 | +// `get?sports=trail_running&sports=road_cycling` binds to [Sport.TrailRunning, Sport.RoadCycling] |
| 88 | +``` |
| 89 | + |
| 90 | +## License |
| 91 | + |
| 92 | +Copyright © 2022 [Dmitry Khmara](https://dmitrykhmara.com). |
| 93 | + |
| 94 | +StrEnum is licensed under the [MIT license](LICENSE.txt). |
0 commit comments