Skip to content

Commit 5c524b3

Browse files
committed
Add the installation and usage instructions to the readme.
1 parent 3ac84a2 commit 5c524b3

2 files changed

Lines changed: 96 additions & 1 deletion

File tree

README.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 &copy; 2022 [Dmitry Khmara](https://dmitrykhmara.com).
93+
94+
StrEnum is licensed under the [MIT license](LICENSE.txt).

src/StrEnum.AspNetCore/StrEnum.AspNetCore.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
<PropertyGroup>
44
<PackageId>StrEnum.AspNetCore</PackageId>
55
<Description>String enum support for ASP.NET Core.</Description>
6-
<Authors>dmitry-khmara</Authors>
6+
<Authors>Dmitry Khmara</Authors>
7+
<Copyright>Copyright Dmitry Khmara</Copyright>
78
<PackageReadmeFile>README.md</PackageReadmeFile>
89
<PackageTags>StringEnum;String;Enum;ASP.NET Core</PackageTags>
910
<PackageIcon>icon.png</PackageIcon>

0 commit comments

Comments
 (0)