Part 1 - Asp Net Core API
Asp.Net Core API project from scratch
The project consist of a full working Asp Net Core API
Requirements
You will need to install the following:
- The latest .NET Core SDK
- Visual Studio 2017 v15.3
What is Asp.Net Core
Asp.Net Core is a cross platform, open source framework for building modern Internet applications.
Asp.Net Core is a complete rewrite of the Asp.Net framework.
NetCore is based a set of granular Nuget Packages resulting in a smaller application surface area, with improved performarce, reduced servicing and better security than is predecessor.
Start
Open Visual Studio e Create a New Project called NetCoreApp
From the list of available template, choose "Empty"
Visual Studio will create the folder with all the initials components for us.
The empty project contains little more than those few elements:
- wwwroot
- Program.cs
- Startup.cs
Asp Net Core Web Applications are essentially console applications and contain a Program.cs just the same as a console app would. This means web apps can now be run from the command line.
Program.cs is the entry point of the web application. It contains settings for hosting the application and other settings.
The following code is the initial content of Program.cs
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup()
.Build();
}
In the Main method of the console application, BuildWebHost is invoked. This method build the host and runs it. The Host runs on top of Kestrel which proxies Http Requests and Responses to IIS Express in our case.
The WebHost instance is set to use Startup.cs where we can put all the application configuration settings.
Starup.cs has two methods: ConfigureServices used to for registering dependency injection and other services, and Configure, used to set the Request Processing Pipeline.
public class Startup
{
// Use this method to add services to the container.public void ConfigureServices(IServiceCollection services)
{
...
}
// Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app)
{
...
}
}
The ConfigureServices method is optional and is called by the web host before the Configure method to configure the app's services. All the configuration options are set by convention.
Adding services to the service container makes them available within the app and in the Configure method. The services are resolved via Dependency Injection or from IApplicationBuilder.ApplicationServices.
The Configure method is used to specify how the app responds to HTTP requests. The request pipeline is made of different software components called middleware. These components are responsible for handling requests and return responses.
In The Empty project Startup.cs, the method Configure contains the following code:
If(env.IsDevelopement){
App.UseDeveloperExceptionPage();
}
app.Run(async (context) =>
{
await context.Response.WriteAsync("Hello World!");
});
The first 3 lines tells to the app to show error pages containing stack trace and all the information
The second code block, writes "Hello World!" in the response stream.
Press F5 and you should see the result in the browser.
Note: In Net Core we can define settings for different environments (development, staging, production).
It’s possible to change the environment using the properties of the Project > Debug > Environment variables. To make an environment active we need to restart IIS
The End
This is the end of the first part.
Happy Programming!