Net 5 Rest API — Controller
So to begin, web API’s are useful tools that can provide functionality to just about every application out there. They can be used to securely handle and send data, to and from clients. While what api type you’ll use rests mainly on your projects requirements, a usual go to in many places I have worked is the Rest Api. Quick and simple to produce and just as easy to deploy.
Just a quick note before I get started, to keep things simple and avoid using external libraries in this project, I cheated and put a list of “users” in the Program class. Don’t do this. Please.
Getting Started:
Create a directory, open a terminal and enter “dotnet new webapi”. That will set up the initial project for you. After that you can start it up by navigating to the folder and using — “dotnet run ”— to start it up, then open a browser and navigate to — https://localhost:5000/weatherforecast - to see if its working. If it is, you will see some weather info appear on your screen. Once you are satisfied, close the browser and open an editor and lets get started…
I won’t cover the startup or program class in this article really, I will however say open up “Startup.cs” and remove “app.UseHttpsRedirection()” as we won’t need it anytime soon. Also inside your project create a directory called “Models” and inside that folder, create a new c# class and name it UserModel(.cs — if you’re using vscode). Add “public string name { get; set; }” to the class, save and close the file.
As you will see, each controller class inherits from an object called “ControllerBase”. In this controller there is an “ILogger” called logger. When the api starts up, all of the parameters are created and passed in by the framework. Unless you make it more complicated. Other than that its pretty straight forward, the only confusing part can be some of the decorators if you’re new to this.
Decorators:
In the image below you can see some of the decorator types available to controller classes. Each decorator essentially flags the app to run specific code when condition x or event y occurs.
ApiController —Signals app that this class is a controller to be used in api.
Route() — Define the endpoint to use to reach this controller.
HttpGet, HttpPost, HttpPut, HttpDelete, etc — Signal app to run a function when a specific crud http request is made.
We also have the “FromBody” decorator. It tells the function to retreive the requests body and converts it into an object, in this case our “UserModel” class.
Launch Settings:
The launch settings are pretty straight forward. In this project I have it set to use my “api/tutorial end point” and it won’t launch the browser on start up either, that can be set by changing launchBrowser to “false”. You can also change the url the project will use at start up as well.
This has been just a quick look at the webapi from .Net Core and .Net 5. It is extremely simply to pick up and very useful in many situations. As always a good tool to keep with you.
Here is a link to a video with the coding in progress -> https://youtu.be/uzo-os3pGfk. Github -> https://github.com/hellzhammer/youtube-medium.
As always, until next time, take care.