Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Developing and Self-Hosting Simple ASP.Net vNext Application without Visual Studio

DZone's Guide to

Developing and Self-Hosting Simple ASP.Net vNext Application without Visual Studio

Free Resource

Add user login and MFA to your next project in minutes. Create a free Okta developer account, drop in one of our SDKs to your application and get back to building.

In this post I will walk you through the latest version of ASP.Net i.e. ASP.Net vNext and talk about some core characteristics as well as some command line tooling options that allows developers to build, run and manage ASP.NET Applications.

ASP.Net vNEXT

ASP.Net vNext is the next version of ASP.Net that comes with some major changes when compared to ASP.NET. It’s open source and comes under .Net Foundation. Now the developers can contribute and get a clone from https://github.com/aspnet

Core characteristics of ASP.Net vNEXT

  • Single programming model for websites (MVC, Web Pages) and services (Web APIs). No more APIController base class for Web API and unified Controller class that serves both MVC Controller and Web API Controller.
  • Introduced Project.json file where developers can define assemblies followed with versions, commands and versions of ASP.Net. Assemblies resolved by the Visual Studio CTP 14 immediately once you save the file and downloaded via NuGet. On the other hand if working in non Visual Studio environment you can use command line KPM restore command to download dependencies.
  • There is no dependency of System.Web.dll.
  • ASP.Net vNEXT is a Cloud optimized framework which is more robust and less in size. The full version of .Net is around 200 MB whereas the cloud optimized one is only 11 MB in size.
  • Dynamic compilation is a big feature provided in Roslyn (.Net Compiler Platform). Now developers can change code and refresh the browser without building the whole project.
  • Side by side support to deploy own version of .NET framework with each application. Each application have its own .net framework version running side by side on single machine.

In ASP.Net vNext, you have an option to run, compile and manage application versions etc. using different command line tooling options. First of all, in order to setup environment for executing KVM, KPM and K commands you have to execute the below powershell script.

@powershell -NoProfile -ExecutionPolicy unrestricted -Command “iex ((new-object net.webclient).DownloadString(‘https://raw.githubusercontent.com/aspnet/Home/master/kvminstall.ps1′))”

Following are the command line tools ASP.NET vNext provides

  • KRE (K Runtime)
  • KVM (K Version Manager)
  • KPM (K Package Manager)
  • K (To run commands)

What is KRE (K Runtime)?

K Runtime contains the SDK tools, compilation system and CLR hosts. It’s an actual ASP.Net vNext runtime that visual studio itself uses to provide intellisense, compilation errors etc.

What is KVM (K Version Manager)?

KVM stands for K Version Manager. This is used to get the K Runtime (KRE) to host ASP.Net vNext applications. This also allows managing the default version on app to app basis. KVM is essential when you want to self-host ASP.Net vNext application and running the self-host web listener using command line tool.

You can install KVM by running this command via command prompt

KVM install 1.0.0-alpha4

On installing KVM, all the kvm files are copied into the current users directory i.e. %windir%:\Users\%username%\.kre folder. It also places the environment variables on windows PATH so you can execute commands without directing to the KVM folder path.

Some widely used KVM commands

  • kvm upgrade

Checks the latest KRE version if available and install it on the machine

By default, we are running full .Net Framework but you can switch to the cloud optimized one using kvm upgrade –runtime CoreCLR

  • kvm list

To list the installed version of KRE the * denotes the active version and you can choose another version on app to app basis using kvm use version command

What is KPM (K Package Manager)?

KPM is the K Package Manager tool that you can use after installing KVM. Through this tool yo

  • kpm restore

To restore the packages defined in the project.json file of your ASP.Net vNext application.

What is K?

K is the command line tool to run ASP.Net vNext application. You can use K run to run the HTTP listener to listen for the incoming request specified in the project.json file.

  • k web

Let’s do some Action!

In this exercise we will develop a simple ASP.Net vNext application without using Visual Studio 14.

  • Create a folder named “SampleKProject” in any of your machine drive.
  • Now create the project.json file inside that folder and copy below content.
{

“webroot” : “wwwroot”,

“exclude”: “wwwroot/**/*.*”,

“dependencies”: {

“Microsoft.AspNet.Diagnostics”: “0.1-alpha-build-0682″,

“Microsoft.AspNet.Hosting”: “0.1-alpha-build-0572″,

“Microsoft.AspNet.Server.WebListener”: “0.1-alpha-build-0520″

},

“commands”: {

“web”: “Microsoft.AspNet.Hosting –server Microsoft.AspNet.Server.WebListener –server.urls http://localhost:8090″

},

“frameworks”: {

“aspnet50″: {},

“aspnetcore50″: {}

}

}

  • If you see the project.json, there are several sections namely webroot, dependencies, exclude, commands, frameworks etc.
webroot To specify the web server root folder where all the static files should be placed. In above code snippet we have specified wwwroot which means all the static files will be placed under wwwroot folder.
dependencies List all the dependent assemblies name followed with version information. All the dependencies will be loaded using kpm restore and downloaded via NuGet.
exclude You can provide folder path to exclude files from compilation
commands When running K command you can pass the name of a command to execute it. For example K web where web is the command name
frameworks To define the target frameworks that will be build or the dependencies that are specific to configuration.
  • Now create the Startup.cs file that is the starting point for ASP.Net vNext application. In this class you can configure the components for the application pipeline. The Startup class must contain a method named “Configure” that takes IAppBuilder as a parameter. Following is a full code snippet of the Startup class.
using System;

using Microsoft.AspNet.Builder;

namespace WebApplication5

{

public class Startup

{

public void Configure(IBuilder app)

{

app.UseWelcomePage();

}

}

}

  • app.useWelcomePage adds a middleware component that shows the welcome page on navigating the URI
  • After creating project.json and Startup.cs files, open up a command prompt and go to the root folder that we created earlier i.e. SampleKProject and run kpm restore. When running the kpm restore it searches the project.json file and resolves the dependencies by downloading them via NuGet.

  • Once this is done you can run k web to start the self-host web listener.

Hint: web server can be stopped by using Ctrl+C and typing ‘Y’ 



Launch your application faster with Okta’s user management API. Register today for the free forever developer edition!

Topics:
dotnet ,visual studio ,kvm ,roslyn ,asp.net vnext ,kre ,kpm ,k ,command line tools

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}