From 4a64163a8a6ae9647b875f95958ca6cf3d3b93b5 Mon Sep 17 00:00:00 2001 From: Dmytro Khmara Date: Sat, 25 Apr 2026 18:07:08 +0100 Subject: [PATCH] Tidy README --- README.md | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 24e2cb7..6edc7d4 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,12 @@ # StrEnum.AspNetCore -Allows to use [StrEnum](https://github.com/StrEnum/StrEnum/) string enums with ASP.NET Core. +Lets you use [StrEnum](https://github.com/StrEnum/StrEnum/) string enums with ASP.NET Core. -Supports ASP.NET Core 6-10. - -ASP.NET Core 5 supported in v2.0.0. +Supports ASP.NET Core 6 – 10. For ASP.NET Core 5, use v2.0.0. ## Installation -You can install [StrEnum.AspNetCore](https://www.nuget.org/packages/StrEnum.AspNetCore/) using the .NET CLI: +Install [StrEnum.AspNetCore](https://www.nuget.org/packages/StrEnum.AspNetCore/) via the .NET CLI: ``` dotnet add package StrEnum.AspNetCore @@ -16,7 +14,9 @@ dotnet add package StrEnum.AspNetCore ## Usage -If you're using `WebApplicationBuilder`, add the call to `AddStringEnums()` into your `Program.cs`: +### Registering the binder + +If you're using `WebApplicationBuilder`, call `AddStringEnums()` in `Program.cs`: ```csharp var builder = WebApplication.CreateBuilder(args); @@ -26,18 +26,18 @@ builder.Services .AddStringEnums(); ``` -If you're using the ASP.NET Core 3.1-5 `IWebHostBuilder`, call `AddStringEnums()` in the `ConfigureServices` method of your `Startup.cs`: +For the ASP.NET Core 3.1–5 `IWebHostBuilder`, call `AddStringEnums()` in `ConfigureServices` in `Startup.cs`: ```csharp public void ConfigureServices(IServiceCollection services) { - services - .AddControllers() - .AddStringEnums(); + services + .AddControllers() + .AddStringEnums(); } ``` -All set. Let's now create a string enum and a model that contains it: +### Defining a string enum and a model ```csharp public class Sport : StringEnum @@ -53,39 +53,41 @@ public class Race } ``` -You can bind string enums to the request body and return them in the response. In your controller, add the following: +### Binding string enums in controllers + +Bind from the request body and serialize them back in the response: ```csharp [HttpPost] -public ActionResult BodyPost([FromBody] Race race) // race.Sport is correctly deserialized +public ActionResult BodyPost([FromBody] Race race) // race.Sport is deserialized correctly { return Ok(race); // race.Sport is serialized back } ``` -You can also bind string enums to a URL: +Bind from a route parameter: ```csharp [HttpGet] [Route("{sport}")] -public ActionResult GetFromRoute(Sport sport) {...} +public ActionResult GetFromRoute(Sport sport) { ... } ``` -To a query string parameter: +Bind from a query string parameter: ```csharp [HttpGet] [Route("get")] -public ActionResult GetFromQuery([FromQuery]Sport sport) {...} +public ActionResult GetFromQuery([FromQuery] Sport sport) { ... } // `get?sport=trail_running` binds to Sport.TrailRunning ``` -And to an array of query string parameters: +Bind to an array of query string parameters: ```csharp [HttpGet] [Route("get")] -public ActionResult GetFromQuery([FromQuery] Sport[] sports) {...} +public ActionResult GetFromQuery([FromQuery] Sport[] sports) { ... } // `get?sports=trail_running&sports=road_cycling` binds to [Sport.TrailRunning, Sport.RoadCycling] ```