Skip to content

Commit 90fb33f

Browse files
authored
Added documentation for 4.x updates and an example for setting up a new web-api project with ApiEndpoints (#209)
1 parent 9a5ccf4 commit 90fb33f

File tree

1 file changed

+146
-8
lines changed

1 file changed

+146
-8
lines changed

README.md

+146-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ A HUGE Thank-You to [AWS](https://github.com/aws) for sponsoring this project in
2020

2121
If you like or are using this project to learn or start your solution, please give it a star. Thanks!
2222

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+
2327
## Upgrade to 4.x Notes
2428

2529
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
5660

5761
[3. Getting Started](#3-getting-started)
5862

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)
68+
69+
- [File Upload Example](#file-upload-example)
70+
5971
[4. Animated Screenshots](#4-animated-screenshots)
6072

6173
[5. Open Questions](#5-open-questions)
@@ -98,15 +110,141 @@ Most REST APIs have groups of endpoints for a given resource. In Controller-base
98110

99111
## 3. Getting Started
100112

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+
var builder = WebApplication.CreateBuilder(args);
138+
var app = 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+
var builder = WebApplication.CreateBuilder(args);
153+
154+
// Add controllers
155+
builder.Services.AddControllers();
156+
157+
var app = 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.
187+
188+
```csharp
189+
public class MyEndpoint : EndpointBaseSync
190+
.WithRequest<string>
191+
.WithActionResult<IActionResult>
192+
{
193+
[HttpGet("my-endpoint")]
194+
public override ActionResult<IActionResult> Handle(string request)
195+
{
196+
// Your logic here
197+
198+
return Ok();
199+
}
200+
}
201+
```
202+
203+
4. **Add Routing Attributes**
204+
205+
Decorate your `Handle` method with `[HttpGet]`, `[HttpPost]`, or other appropriate HTTP method attributes, specifying the route for your endpoint.
206+
207+
5. **Define Request and Response Types**
208+
209+
Define your `TRequest` and `TResponse` types in the same file as your endpoint class or in separate files as per your project structure.
210+
211+
6. **Test Your API Endpoint**
212+
213+
Test your ASP.NET Core API endpoint. If you're using Swagger/OpenAPI, it should automatically document your endpoint based on the attributes provided.
214+
215+
#### Example Endpoint Class
216+
217+
Here's an example of a GET endpoint that returns a list of books using `Ardalis.ApiEndpoints`:
218+
219+
```csharp
220+
public class ListBooksEndpoint : EndpointBaseSync
221+
.WithoutRequest
222+
.WithResult<IList<BookDto>>
223+
{
224+
private readonly IRepository<Book> repository;
225+
private readonly IMapper mapper;
226+
227+
public ListBooksEndpoint(
228+
IRepository<Book> repository,
229+
IMapper mapper)
230+
{
231+
this.repository = repository;
232+
this.mapper = mapper;
233+
}
234+
235+
[HttpGet("/books")]
236+
public override IList<BookDto> Handle()
237+
{
238+
var books = repository.ListAll();
239+
240+
var bookDtos = books.Select(book => mapper.Map<BookDto>(book)).ToList();
241+
242+
return bookDtos;
243+
}
244+
}
245+
```
102246

103-
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.
110248

111249
### Adding common endpoint groupings using Swagger
112250

0 commit comments

Comments
 (0)