Open a terminal and use dotnet --help
to see available commands. Additionally, use dotnet new --help
to view options available to the new project command.
Run the following command to view every type of project template for your SDK:
dotnet new --list
A solution is a collection of projects. It is the glue that binds all of the individual projects into a logical unit.
dotnet new sln
ASP.NET Web API is a framework for building HTTP services that can be accessed from any client including browsers and mobile devices.
dotnet new webapi -o API
The -o
flag builds the files in a new directory called API
Then add the Web API to the .NET solution:
dotnet sln add API/
The project directory should look something like this:
Solution
/API
/Dependencies
/Properties
launchSettings.json
/Controllers
WeatherForecastController.cs
appsettings.json
appsettings.Development.json
Program.cs
WeatherForecast.cs
Within the API folder, there should also be the project properties file or .csproj
file for API. In IDE’s this may be hidden but can be accessed by editing the API directory itself. If this is the case, right click the API directory and look for Edit. This example was made for .NET version 6, so it may change in the future.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>
Open Properties/launchSettings.json. By default, running the program will open a new browser window and launch Swagger to view the API. To change this so that you will just see the raw API data, change the following:
launchSettings.json
...
"profiles": {
"API": {
...
"launchBrowser": true,
"launchUrl": "weatherforecast",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
...
},
...
}
This will switch the browser start up to /weatherforecast on port 5000 and 5001.
This is also where you can configure whether or not a browser will launch when you run the app. change the launchBrowser
option to false
.
There are two files for app settings: appsettings.json
and appsettings.Development.json
. The development file is best for information that does not need to be private. For set up configurations, open appsettings.Development.json
. By default, the console will output all Warning level messages, but for developing an API, it is nice to see the responses when interacting with the REST services. To enable this, change the file to the following:
appsettings.Development.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Information"
}
}
}
Connection Strings
To add a construction string (for a database connection), add the following:
{
"ConnectionStrings": {
"DefaultConnectinon": "Data source={dbfile}.db"
},
"Logging": {
...
}
}
where dbfile is the name of the database file.
Now lets dive into the c# files that make the app work.
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
The Program class is the starting point of the application. To be more exact, the Main Method within the class is the starting point. In later versions of .NET, the namespace and class definitions are negated. This is part of the Top-Level-Statement feature introduced in .NET 6. The namespace, includes, and method names will be generated automatically during the compile time.
Lets breakdown the parts of the Project.cs code.
var builder = WebApplication.CreateBuilder(ags)
- The application needs a builder and because we are using ASP.NET, we will use a WebApplicationBuilder.builder.Services.*
- Once we have a builder, the services collection on that builder is where we can add services to use during run time. This is considered a build dependency injection container. By adding services to the container, we can use them wherever we need them. AddEndpointsApiExplorer
and AddSwaggerGen
are part of the Swashbuckler’s Swagger API Documentation.var app = builder.Build();
- This resolves a WebApplication type object which implements IApplicationBuilder
app.*
- Instances of classes that implement IApplicationBuilder
provide the mechanisms to configure an application’s request pipeline. By doing this, we can specify how an ASP.NET Core application will respond to individual HTTP requests. The components that potentially handle these requests are called middleware.Middleware is defined as software components that assembled into an application pipeline to handle requests and responses. Example of middleware could include diagnostics or authentication. The Request Pipeline will requests delegates to process each middleware component, one after the other, from one piece of middleware to the next. Each will have the chance to perform operations before and after the next delegate, eventually resulting in a response. Because each middleware component chooses whether or not to pass on the delegate, the order of the middleware matters.