Introduction To ASP.NET Core

Introduction

Nowadays, everyone is talking about open source, cross platform development. Microsoft has always been known for its Windows based products, but now we are in the new age of development. For this, a new revolutionary product came into the market, which is Microsoft .NET Core.

.NET Core is a free open source and cross platform framework, created for building modern Cloud based Applications and every .NET developer feels proud of it. Now, there are no boundaries for the platform. Now, every .NET developer can say, yes I am platform independent, I am using an open source.

In the revolution of software development, Microsoft launched its first .NET framework in the year 2000 with its first .NET framework 1.0. This framework plays a major role in the field of software development. People love Microsoft technology products, because these products are easy to use and easy to learn.

When first time .NET framework came in to the market, it came with ASP.NET, C# and VB.NET, Web Services, multithreading, assembly and many things under this bucket. As time went by, .NET framework came with its many versions like 1.0,1.1, 2.0, 3.5,4.0,4.5, 4.6 etc. but all these frameworks support a particular platform and the platform was Windows. If I am talking about my journey, I got the chance to work in all the versions of .NET framework.

In the year 2016, Microsoft has come up with a new revolution, Microsoft .NET Core 1.0. People always love ASP.NET, because it's working over WorldWideWeb. Today in this article, we will learn about ASP.NET Core as an introduction.

ASP.NET Core is a new open source, cross platform framework to create modern Web based Cloud based systems, which means, now you are not only working for Windows, you can run in Linux, Mac; i.e., wherever you want.

ASP.NET Core features

  1. Cross platform, open source now runs your app over Linux, Windows, Mac;   i.e., wherever you want.
  2. Fast Development- fast work over the Browsers.
  3. Work in your editors - now you can work not only in Visual Studio. You can also choose Visual Studio code and if you want to work on your command on the command prompt, then you can.

    website

Now, the question arises, where you can download .NET Core, so here is the Website as you just need to log on to http://dot.net

website

Here, you can download Visual Studio 2015, .NET Core and Xamarin very easily.

Now, I am going to tell you what comes in .NET framework and what comes in .NET Core.

.NET framework includes

  1. ASP.NET 4.6 (System.web)
  2. Web Forms
  3. MVC
  4. Web API

.NET framework is known for Windows platform.

.Net Core comes with

  1. ASP.NET Core MVC
  2. ASP.NET Core 1.0 (Microsoft.ASPNETCore)

.NET Core is known for cross platform developments. Now, it's up to you, what’s your requirement, if you want to create apps, which can run over different platforms then choose .NET Core. If you are happy with Windows, you have .NET framework.

If you had already downloaded .NET Core from .NET Website (if not please download, it's free), so let’s do some coding on .NET Core.

First of all, if you want to know about your dotnet core info, just open the command prompt and type dotnet for example - C:\>dotnet and press enter.

website

You can see currently we have version 1.0.1.

If you had already worked on .NET framework, you always create your apps on Visual Studio. Today, I am going to tell you that you can create new .NET Core projects on the command prompt, then type yes in the command prompt.

Lets see how you can.

  1. C:\>Md testCore - this will create a new directory.
  2. C:\>cd testcore - this will enter you into the directory.
  3. now type c:\>dotnet new - this will create a new project.
  4. C:\testCore> dotnet new

Created new c# project in c:\testCore>

Now, open this Project over Visual Studio Code or Visual Studio. It's a simple C# app, which looks like this in Solution Explorer.

website

Let’s understand one by one its file structure.

  1. Program.cs
    This is a normal HelloWorld console app. Here, you can change as per your requirement.
    1. using System;  
    2. namespace ConsoleApplication {  
    3.     public class Program {  
    4.         public static void Main(string[] args) {  
    5.             Console.WriteLine("Hello World!");  
    6.         }  
    7.     }  
    8. }  
  2. project.json
    This is JSON, which is going to tell you if your app is dependent on these dependencies. Hence, here you will add your future dependencies for an app.
    1. {  
    2.     "version""1.0.0-*",  
    3.     "buildOptions": {  
    4.         "debugType""portable",  
    5.         "emitEntryPoint"true  
    6.     },  
    7.     "dependencies": {},  
    8.     "frameworks": {  
    9.         "netcoreapp1.0": {  
    10.             "dependencies": {  
    11.                 "Microsoft.NETCore.App": {  
    12.                     "type""platform",  
    13.                     "version""1.0.0"  
    14.                 }  
    15.             },  
    16.             "imports""dnxcore50"  
    17.         }  
    18.     }  
    19. }  
  3. project.lock.json
    This is the full graph of your package.

    Now, if you want to run your code press F5 or in the command prompt, write .NET and run it. If you are under folder in your command prompt, your app will run.

    website

    This is your first Hello app, you can test this app over Windows, Mac, Linux, as it  up to you, because you have the power of cross platform.

    Now, we will create our first ASP.NET Core app. For this, we will add some dependencies in project JSON file.

    Add the code, mentioned below in to the dependencies section in project.json
    1. "dependencies": {  
    2.     "Microsoft.AspNetCore.Server.Kestrel""1.0.1"  
    3. },  

What is Kestrel

Kestrel is a Webserver to run ASP.NET Core Applications. This is the reason due to which we will add the dependency, mentioned below.

Now create a host for this server, into program.cs

  1. using Microsoft.AspNetCore.Builder;  
  2. using Microsoft.AspNetCore.Hosting;  
  3. using Microsoft.AspNetCore.Http;  
  4. using System;  
  5. namespace ConsoleApplication {  
  6.     public class Program {  
  7.         public static void Main(string[] args) {  
  8.             var host = new WebHostBuilder().UseKestrel() //Server  
  9.                 .Configure(app => {  
  10.                     app.Run(context => context.Response.WriteAsync("Hello in Asp.net Core"));  
  11.                 }).Build();  
  12.             host.Run();  
  13.         }  
  14.     }  
  15. // This is just a sample script. Paste your real code (javascript or HTML) here.  
  16. if ('this_is' == /an_example/) {  
  17.     of_beautifier();  
  18. else {  
  19.     var a = b ? (c % d) : e[f];  
  20. }  
As you can see in the code mentioned above, we have a host object with WebHostBuilder class and we configure app logic, build and run our host. Now, Press F5 and run the Project. Once you run the project, the screen looks, as shown below.

website

As you can see, your Application is hosted and you have a URL, which is localhost:5000. Now, open your Web Browser and type the URL and run it.

website

You can see our first ASP.NET Core app is running over the Web Browser. This was a simple introduction to ASP.NET Core. If you have any queries regarding this article, please write your comment in the comment box. Happy programming.