DEV Community

Cover image for Building REST APIs In .NET 8 The Easy Way!

Building REST APIs In .NET 8 The Easy Way!

Dĵ ΝιΓΞΗΛψΚ on January 05, 2022

The goal of this article is to introduce you to an alternative, more developer friendly way of building Web APIs with ASP.NET 8 instead of the more...
Collapse
 
renamed profile image
Renato Moura Martins Medeiros

A good article, but I thought there would be a series of one explaining other topics, such as dealing with login, jwt and so on.

Collapse
 
djnitehawk profile image
Dĵ ΝιΓΞΗΛψΚ • Edited

planning to write those articles in the future as time permits. however, the documentation is quite easy to follow in the meantime if you're interested. thanks!

Collapse
 
msoffsite profile image
Mark Stone

Yeah, no, it isn't. I've successfully generated a token and am trying to access a simple endpoint without request that is a get and returns only a string. I followed the example given at fast-endpoints.com/docs/security#s.... JWT authorizes fine but I am getting a 401 and 403. According to fast-endpoints.com/docs/swagger-su..., because AllowAnonymous isn't within the configure method and the 403 because something is forbidden but who knows what.

Thread Thread
 
djnitehawk profile image
Dĵ ΝιΓΞΗΛψΚ

probably a minor misconfiguration issue. pls create a support ticket with repro code either on discord or github. we'll get you sorted 😉

Thread Thread
 
msoffsite profile image
Mark Stone

I was able to get it fixed. Turned out it was the policy portion that messed me up a bit.

Thread Thread
 
rahiyansafz profile image
Rahiyan Safin

can you share how you solved it?

Collapse
 
alias111 profile image
Jose Martinez • Edited

This tutorial needs to be updated. Its due.
I got tired of going back and forth from this and the docs.
though this is a much deeper example then the docs i think
its time for an update that aligns with where the docs are at.
I see the use of a mapper in this article but in the docs its done differently with a brief example of how, if by any chance you create a mapper in this manner. Good thing the github is linked also but still needs an update. Hope its soon. Thanks for the article. Im not debating if its better then mvc or any of that. Im coming from js/ts with node/bun, so just the fact your using any api architecture for the folder structure is amazing. I also use brackets on the same line. So i know thats .Net blasphamy lol. I just need a solid update to this article or inbed this version of the tutorial in the docs. The current one is way simpler then this and this one seems the better of the two because of the detail. Just a suggestion.

Collapse
 
dadapipes profile image
dadaPipes

dotnet new web -n MiniDevTo
dotnet add package FastEndpoints
dotnet add package FastEndpoints.Swagger
dotnet add package MongoDB.Entities
dotnet add package BCrypt.Net-Next ( I needed this to make it work )

Collapse
 
tmac profile image
tmac

This is all very interesting, but what is the purpose that I would move into "FastEndpoints" vs. MediatR? Right now, ALL of our Controllers are 1 line of code each "action", meaning, there is absolutely no need to UnitTest a Controller, and the only thing the controller is there for is "decorating" the API for swagger. I could see other developers getting quickly confused why there is so much orchestration for setting up an API?

Example Controller action:
[HttpPost("transaction")] // /inventory/transaction
[Produces(System.Net.Mime.MediaTypeNames.Application.Json)]
[ProducesResponseType(StatusCodes.Status202Accepted)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
public async Task SaveInventoryTransaction(InventoryTransaction model)
=> await this.Execute(
new SaveInventoryTransactionEvent { Model = new InventoryTransaction[] { model } },
// Return 202, not 200 because 200 means we've processed it, whereas 202 means we've accepted it for processing later
res => this.Accepted()
);

Collapse
 
djnitehawk profile image
Dĵ ΝιΓΞΗΛψΚ

give this article a read: bradjolicoeur.com/Article/fast-end...

Collapse
 
arizvitechgies profile image
arizvi-techgies

Nice! I was wondering if there is a way to separate the Features as a project.

For example:

MyDemoApp - Solution
MyDemoApp.API - CSPROJ (ASPNET Core Project)
MyDemoApp.Features.Admin - CSPROJ (Class Library)
MyDemoApp.Features.Client - CSPROJ (Class Library)

Each Feature project contains its own Endpoints and features folder structure, as shown in your blob.

Is there a way to bring all those Features Project .dll into MyDemoAPp.API and registered them in Startup.cs, so does Fast Endpoint pick them up?

Collapse
 
djnitehawk profile image
Dĵ ΝιΓΞΗΛψΚ

it's possible. you can add assemblies that contain endpoints for discovery in the .AddFastEndpoints(...) call. hit us up in our discord channel if you have trouble getting that sorted.

Collapse
 
thetrigger profile image
ƒabio

Hello, how can I use app.UseFastEndpoints with IApplicationBuilder instead of WebApplication? since i'm not using a minimal API

Collapse
 
thetrigger profile image
ƒabio

Nvm, the solution is

app.UseFastEndpointsMiddleware();
app.UseEndpoints(endpoints =>
{
    endpoints.MapFastEndpoints();
Enter fullscreen mode Exit fullscreen mode