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:

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!

To view or add a comment, sign in

More articles by Enrico A

  • Microservices and Keycloak

    First experiment on Keycloak The purpose of my first experiment on Keycloak was to study and evaluate a new open source…

    4 Comments
  • Parse JWT tokens in React.js

    While working on a small React.js client for Identity Server 4, I found this small but very handy function to decode…

  • TSQL - Check if it's a Guid

    SELECT your_column FROM   your_table WHERE  TRY_CONVERT(UNIQUEIDENTIFIER, your_column) IS NOT NULL; Happy programming!

  • Part 6 - Asp Net Core API from scratch

    In this article we will see how to persist our data using Entity Framework Core. This is a continuation to Part 5…

  • Part 5 - Asp Net Core API from scratch

    In this article we will see how to improve our Asp.Net Core API using services and Dependency Injection.

  • Part 4 - Asp Net Core API from scratch

    In this article we will see how to Create, Read, Update and Delete child resources in our Asp.Net Core API.

  • Part 3 - Asp Net Core API from scratch

    In this article we will see how to make our Asp.Net Core API return resouces.

  • Part 2 - Asp Net Core API

    This is continuation to Part 1 - Asp.Net Core API project from scratch Contents In this part we will see: - Project…

  • Asp.Net Web API Pdf Download utility

    Orginally posted on Monday, December 12, 2016 http://enricoacampora.co.

  • Umbraco Image Cropper and Bootstrap Carousel

    In this article we will see how to use the Umbraco Image Cropper in combination with the Twitter Bootstrap Carousel…

Insights from the community

Others also viewed

Explore topics