Tuesday, October 11, 2016

Asp .Net Core Part 2 : Using Visual Studio

Visual Studio 2015 is the most preferred tool for creating Asp .Net Core Applications. I am starting an Asp .Net Core Web Application from New Project option.


Staring with an Empty project so that we can gradually build the application instead of getting overwhelmed with a plenty of options.


In the Solution Explorer we can see project.json which we already explore in Part 1,




When I run the application I get the Hello World output.





public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.Run(async (context) =>
            {
                await context.Response.WriteAsync("Hello World from Shalvin!");
            });
        }

I am making alteration in WriteAsync method within Configure method.

By running the application again you can see the new output.





Asp .Net Core Part 1 : dotnet tool and Console application


Asp .Net Core is an cross platform and open source MVC framework from Microsoft. Asp .Net core is characterized by  low memory footprint.


First let's create a Console application with Asp .Net Core.  Asp .Net Core gets  installed as a part of Visual Studio Update 3.
Asp .Net Core is not tied to Visual Studio. You can use any editor for creating Asp .Net Core Application.

Here I am going to demonstrate creating a simple Console application using dotnet tool.

 I can go to command prompt and type dotnet --help.


dotnet  is the .Net Command Line Tool.

For creating an new project we can use the comman dotnet new.


The project is having two files: Project.json and Program.cs.

Project.json file is having all the dependencies. 

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        }
      },
      "imports": "dnxcore50"
    }
  }
}

Program.cs is having the actual code.

using System;

namespace ConsoleApplication
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

dotnet restore command is used to restore the project with dependencies form Nuget based on the entries in Project.json,

We can see the output with dotnet run command.