You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+146-8
Original file line number
Diff line number
Diff line change
@@ -20,6 +20,10 @@ A HUGE Thank-You to [AWS](https://github.com/aws) for sponsoring this project in
20
20
21
21
If you like or are using this project to learn or start your solution, please give it a star. Thanks!
22
22
23
+
## Discover a Practical Example
24
+
25
+
If you're eager to dive into a practical example of using `Ardalis.ApiEndpoints`, check out our [Getting Started guide](#3-getting-started). This guide walks you through setting up your environment and creating your first API endpoint using the latest features in version 4.x.
26
+
23
27
## Upgrade to 4.x Notes
24
28
25
29
The fluent generics and base types involved in ApiEndpoints were updated in version 4.x, resulting in breaking changes. The updates required should be pretty straightforward, and have a few additional features that weren't supported in previous versions.
@@ -56,6 +60,14 @@ For version 3.0 we implemented a new way to define the base classes using "fluen
56
60
57
61
[3. Getting Started](#3-getting-started)
58
62
63
+
-[Set Up an Empty Web API Project](#set-up-an-empty-web-api-project)
64
+
65
+
-[Adding Controllers with Ardalis.ApiEndpoints](#adding-controllers-with-ardalisapiendpoints)
66
+
67
+
-[Adding common endpoint groupings using Swagger](#adding-common-endpoint-groupings-using-swagger)
@@ -98,15 +110,141 @@ Most REST APIs have groups of endpoints for a given resource. In Controller-base
98
110
99
111
## 3. Getting Started
100
112
101
-
I'll look to add detailed documentation in the future but for now here's all you need to get started (you can also check the sample project):
113
+
### Set Up an Empty Web API Project
114
+
115
+
#### Overview
116
+
117
+
When starting a new Web API project using .NET, you might want to begin with an empty project structure to have more control over dependencies and configurations.
118
+
119
+
#### Setting Up the Project
120
+
121
+
1.**Create the Project:**
122
+
123
+
Start by creating a new empty web API project. You can do this using the .NET CLI:
124
+
125
+
```bash
126
+
dotnet new web -n MyWebApi
127
+
cd MyWebApi
128
+
```
129
+
130
+
This creates a basic project structure for a web application.
131
+
132
+
2.**Update Program.cs:**
133
+
134
+
Open the `Program.cs` file, which serves as the entry point for your application. By default, it might contain the following code:
135
+
136
+
```csharp
137
+
varbuilder=WebApplication.CreateBuilder(args);
138
+
varapp=builder.Build();
139
+
140
+
// app.MapGet("/", () => "Hello World!");
141
+
142
+
app.Run();
143
+
```
144
+
145
+
Remove the `app.MapGet("/", () => "Hello World!");` line as it sets up a minimal endpoint which we'll replace with controller routing.
146
+
147
+
3.**Configure Services and Routing:**
148
+
149
+
Modify the `Program.cs` file to include controller services and routing:
150
+
151
+
```csharp
152
+
varbuilder=WebApplication.CreateBuilder(args);
153
+
154
+
// Add controllers
155
+
builder.Services.AddControllers();
156
+
157
+
varapp=builder.Build();
158
+
159
+
// Map controllers to endpoints
160
+
app.MapControllers();
161
+
162
+
app.Run();
163
+
```
164
+
165
+
-**`builder.Services.AddControllers()`**: Adds services required for controllers to handle HTTP requests.
166
+
-**`app.MapControllers()`**: Maps controllers to appropriate endpoints based on routing attributes and conventions.
167
+
168
+
These two methods are required for the endpoint to work.
169
+
170
+
### Adding Controllers with Ardalis.ApiEndpoints
171
+
172
+
1.**Install Ardalis.ApiEndpoints NuGet Package**
173
+
174
+
Add the `Ardalis.ApiEndpoints` package to your ASP.NET Core web project. You can do this using the NuGet Package Manager or via the .NET CLI:
175
+
176
+
```bash
177
+
dotnet add package Ardalis.ApiEndpoints
178
+
```
179
+
180
+
2.**Create Endpoint Classes**
181
+
182
+
Create your endpoint classes by inheriting from `EndpointBaseSync` or `EndpointBaseAsync`, and add `.WithRequest<TRequest>` or `.WithResult<TResponse>`, or both, depending on whether your endpoint accepts input (POST) or simply returns a response (GET).
183
+
184
+
3.**Implement Handle Method**
185
+
186
+
Implement the `Handle` method from the base class (`EndpointBaseSync`) in your endpoint class. This method contains the logic to process the request and return the response.
1. Add the [Ardalis.ApiEndpoints NuGet package](https://www.nuget.org/packages/Ardalis.ApiEndpoints/) to your ASP.NET Core web project.
104
-
2. Create Endpoint classes by inheriting from either `EndpointBaseSync<TRequest,TResponse>` (for endpoints that accept a model as input) or `EndpointBaseSync<TResponse>` (for endpoints that simply return a response). For example, a POST endpoint that creates a resource and then returns the newly created record would use the version that includes both a Request and a Response. A GET endpoint that just returns a list of records and doesn't accept any arguments would use the second version.
105
-
3. Implement the base class's abstract `Handle()` method.
106
-
4. Make sure to add a `[HttpGet]` or similar attribute to your `Handle()` method, specifying its route.
107
-
5. Define your `TResponse` type in a file in the same folder as its corresponding endpoint (or in the same file if you prefer).
108
-
6. Define your `TRequest` type (if any) just like the `TResponse` class.
109
-
7. Test your ASP.NET Core API Endpoint. If you're using Swagger/OpenAPI it should just work with it automatically.
247
+
This endpoint demonstrates listing books and uses the `HttpGet` annotation.
110
248
111
249
### Adding common endpoint groupings using Swagger
0 commit comments