This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Blake Lamb
Overview
Setting up a Web API using C#/.NET is pretty easy, setting up endpoints and connecting to a database can be more difficult. This article will go through the process of how to accomplish those tasks using vsCode.
Setup
Download the .NET SDK
In order to run a .NET Web API, the .NET SDK has to be downloaded. This is pretty easy, just click here for the official download page.
Create A Sandbox App
Now that the SDK is installed, open up a run a new instance of vsCode and navigate in the command line to the folder where the API will live. Once there, use the command dotnet new webapi
. Once that has completed running, the WebApi has been created. I will walk through a few more setup steps before we get into the rest of the API work.
Installs
These are some required and some useful extensions to have downloaded in vsCode:
- c# by Microsoft (Required)
- NuGet Package Manager by jmrog/li>
- Visual Studio IntelliCode by Microsoft
Now that these have been installed, run dotnet add package MongoDB.Driver
Set a port for the WebApi to run on
Find the launchSettings.json file in your root folder and open it. Locate the line with the application name, then below that, find the line that says applicationUrl. Change this line to whatever url you would like it to run on. I have mine set to run on localhost:4000
.
// launchSettings.json
"daily_debrief.API": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:4000;",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
That is all the setup that is needed. Your WebApi is now configured and ready to run! Go to the terminal and run dotnet run
. If it's successful, there should be a message saying that it's listening on the url that was specified earlier.
Creating A Controller With An Endpoint
Now, it's time to make the WebApi function by adding an endpoint. First we will need to create a Controllers folder in the root of the project. Inside the Controllers folder, create a new file named UserController.cs. In the UserController.cs file paste this code as the template:
// userController.cs
using Microsoft.AspNetCore.Mvc;
using System.Text.Encodings.Web
namespace test.API.Controllers
{
[ApiController]
[Route("api/users")]
public class HelloWorldController : ControllerBase
[HTTPGet("test")]
public string Test()
{
return "This is a test endpoint";
}
}
Stop and restart the server, then go to the browser and type in the serverUrl with 'api/users/test' appended to the end. It should return the message from in the test method that was just created.
Creating A Model
A model is a structure that will show the shape of data that will be returned by an endpoint. First, create a folder in the root of the project named 'Models'. All models will live here. Inside that folder create a file called User.cs. Inside the User.cs file, add the following code:
// User.cs
using System;
namespace daily_debrief.API
{
public class User
{
public DateTime created { get; set;
public string FirstName { get; set;
public string LastName { get; set;
public string EmailAddress { get; set;
public string Username { get; set; }
}
}
Then go to the UserController.cs file and add using test.API.Models;
to the top of the file and the following code below the existing endpoint:
UserController.cs
HttpGet("testUser/{email}")]
public IActionResult GetUser(string email)
{
User testUser = new User()
{
created = DateTime.Now,
FirstName = "Blake",
LastName = "Lamb",
EmailAddress = email,
Username = "blamb31"
};
return Ok(testUser);
}
After stopping and restarting the server again, you can now call the endpoint '/api/users/test/blake@blakelamb.com' and see the return of a User object with that email.
Recap
C#/.NET can be a good language to use as the backend of an app. With some templates that already come with C#/.NET it's pretty simple to get a server up and running. Adding controllers, endpoints and models is also simple and allows data to be passed back to the user easily.
This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by Blake Lamb
Blake Lamb | Sciencx (2022-12-02T19:07:20+00:00) Creating a C# Web API. Retrieved from https://www.scien.cx/2022/12/02/creating-a-c-web-api/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.