Wednesday, 2 November 2016

Building your first asp.net core application.

Introduction

In this post, we are going to get started with building an asp.net core application using visual studio 2013. As my last article stated aspnet core is meant to work in visual studio 2015, but in this post, I will show you how to develop asp.net core application using using visual studio 2013. So here is a list of things we have to do before getting started:

  1. First update your .net framework to the latest version (as of this writing version 4.6.2).
  2. Download and install .net targeting pack for .net framework 4.6. Dot net targeting pack enables you to build application targeting .net framework 4.6 in visual studio 2013 and visual studio 2012 or other third-party ides.
  3. Create a Windows Console Project.
  4. Create a file named project.json in the root of the project and add the following code snippet
{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {},
  "frameworks": {
    "netcoreapp1.0": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.0.0"
        },
        "Microsoft.AspNetCore.Server.Kestrel": "1.0.0"
      },
      "imports": "dnxcore50"
    }
  }
}

Then install the following nuget packages:

  1. Microsoft.Aspnetcore.Hosting
  2. Microsoft.Aspnetcore.Server.Kestrel
After that, add a Startup.cs file to the root of your application with the codes below:

public class Startup
{
    public void Configure(IApplicationBuilder app)
    {
        app.Run(context =>
        {
            return context.Response.WriteAsync("Hello from ASP.NET Core!");
        });
    }
}

Then update the main method of your console application:

public class Program
{
    public static void Main(string[] args)
    {
        var host = new WebHostBuilder()
            .UseKestrel()
            .UseStartup<Startup>()
            .Build();

        host.Run();
    }
}

After that, right-click on the project in your solution explorer, select "Add>Existing Item". Then navigate the following path [solution root]\packages\Libuv.[version]\runtimes\[os]\native. Then select libuv.dll. After adding the dll to your application, right-click on it and select properties, then change "Copy to output directory" to "copy always". Then you're good to go.

Conclusion
In this post, I have shown you how to create an asp.net core application using visual studio 2013. In the next series of posts, we will have a look at the underside of asp.net core and at what can be achieved with it. 

No comments:

Post a Comment