Up and running with ASP.net vNext
Setting up environment, writing c# code in Brackets and running in command prompt.
For so long now you have been working on OSX and UNIX, but
want to taste some Microsoft’s cutting edged technologies. By which I mean you
want to end up writing some code in C#. Now the burning question is, how am I
going to even write C# code without Visual Studio? And moreover my OS doesn’t
support running Microsoft’s products. Well! You don’t have to whine anymore
because I’ve got a good news for you. If
you attended the Microsoft’s virtual event called Connect(); earlier November
2014, you may have come across knowing that ASP.net 5 has gone open source. It
is known as ASP.net vNext.
Event videos can be found in this link below
Link to the GitHub repository for ASP.net vNext is down
below
If you’ve already browsed the repository and read the
documentation, you may have seen the minimum requirements and tools to run
ASP.net vNext applications in OSX and UNIX operating systems.
In short all you have to do is to install two things in
order to create an environment in which you can run C# applications. One of
those is KVM (K Version Manager) which basically provides you with .net
framework libraries. And the second one is KRE (K Runtime Environment) which is
just a runtime environment provider for the framework. It’s just like you have
to install JDK and JRE for building and running JAVA applications. So all the
hard work is done and time to write some C#.
Before that lets say I don’t have Visual Studio installed in
my windows (Of course you can’t install this mind blowing IDE in OSX and UNIX).
However I want to take the full advantage of IntelliSense supported in Visual
Studio. Let’s take some tiny steps in building our own Visual Studio. First of
all choose any advance text editing tool you like. Not just any though, to have
the full IntelliSense support you’ve to choose between these advance text
editing tools. Don’t worry Sublime Text is one of them and the rest are
· Atom
· Brackets
· Emacs
· Vim
I
choose Brackets since it has some features that fits my needs. Okay so I’ve
installed Brackets and fired it up. Here what you will see at the first run.
Let’s
click on the icon that says Extension Manager. In this window you can install
some extensions for Brackets which will ultimately make your development life
easier.
So
why would I go to the Extension Manger window? Of course for installing an
extension. But which one? The name of the extension is OmniSharp.
You can visit
the official website of this project by going to
As
you will see the tag line, it says
OmniSharp
- Cross platform .NET development
.NET
in Your Editor of Choice
So
it’s just an extension provider for your favorite text editor which will enable
the IntelliSense support for .net applications. It provides more features than
just the IntelliSense support. I leave the rest for you to discover. So far so
good.
You
are almost ready, in windows let’s run the command prompt in administrative
mode. Copy and paste the following command and hit enter
@powershell -NoProfile -ExecutionPolicy unrestricted
-Command "iex ((new-object
net.webclient).DownloadString('https://raw.githubusercontent.com/aspnet/Home/master/kvminstall.ps1'))"
This
command will install the KVM (k Version Manager) in your system.
Again
to install the KRE (K Runtime Environment), fire up another command prompt and run the following command
kvm
upgrade
Now
clone the Asp.net vNext home repository into your desktop.
If
you browse the Home folder, you will find three subfolders under the samples
folder. These are
· HelloMVC
· HelloWeb
Well,
if you want to write a console application you will work in the ConsoleApp
folder, the HelloMVC is for creating an Asp.net MVC app and the HelloWeb is
just a basic web application skeleton which will just show a static page if you
run it.
Now
let’s make a traditional “Hello World” application. Before that let’s go inside
of the ConsoleApp folder from the command prompt and run the command below
kpm
restore
It
will restore the packages required by that sample. All the command does is to
simply look at the project.json file and download and add the dependencies
provided there. Just like nuget package manager.
Now
open the ConsoleApp folder in brackets and modify the Program.cs file inside
that. Change the text inside the Console.WriteLine method to “Hello from ASP.NET vNext!” and
save it.
Finally
it’s time to run the console application. To do that just type the command
below in the command prompt
k
run
It
will build and run the console application inside the command prompt and you
will get your “Hello from ASP.NET vNext!” string printed on the console.
Building and Running an Asp.net MVC application
Just like the console application, let’s change our
directory and go inside HelloMVC folder and run the command below to restore
the packages required by the sample.
kpm
restore
Now open the folder in Brackets and you will see a MVC
application skeleton with a simple POCO class named User inside the Models
folder, a HomeController inside the controllers and Index.cshtml inside the
Home folder under the Views folder. We also have a _Layout.cshtml file under
shared folder which basically just works like a master page for your
application.
Rather wasting time, let’s create a POCO class of our own
called Employee and add this properties down there.
Now create a controller named EmployeesController and define
an Index method which will be responsible for returning a list of predefined
employees into the view.
Time to create the view in which we will show the list of
employees in a tabular manner. Create an Employees folder under Views and add
an Index.cshtml file there. Write the code below
Our
simple MVC application is ready to run. Running a web application is slightly
different from running a console app, instead of k run you will have
to type the command below and hit enter
k
web
The command will create a server and will an open a
listening port on http://localhost:5001
Open your favorite browser and browse the link. It will show
you the home page. Let’s see our Tabular Employee list. For that go to http://localhost:5001/Employees . There you will see your Tabluar employee list.
So here you go. I’ve shown you how you
can both modify, build and run your own Console and ASP.net MVC application
with the help of ASP.net vNext.
I hope you enjoyed the post. I’ll come
again with interesting posts like this. Till then Happy Coding and Happy New
Year!