Hello ASP.NET 5 World – Part I

First of all, Visual Studio 2015 CTP 6 was released this week and with it the beta 3 of CoreCLR and ASP.NET 5. I’ll be using beta 3 for this post. If you’re not installing VS, make sure you re-install KVM, because there were a few changes (different layout on the file system, new commands on KPM, etc.).

Programming model

In my previous post I wasn’t clear about one thing: ASP.NET’s 5 side-by-side deployment, pay-for-play and multi-host approach also means that the monolithic System.Web is gone. No more IHttpHandler, IHtpModule and co. Of course many of the features on System.Web will have its counterpart on ASP.NET 5 stack.

ASP.NET 5 is OWINish. In fact, the Katana project was integrated into ASP.NET 5, defining the base programming model. This means we’ll deal with middlewares, AppFunc and the enviroment dictionary. Well, actually the ASP.NET 5 HTTP object model has a higher abstraction level rather than exposing the raw OWIN concepts.That’s why I said it is “OWINish”.

Hello World!

So, what does it take to have a simple “Hello World” in ASP.NET 5? It’s actually quite simple. I found this nice tutorial with all the steps to set up from the ground, so I won’t reproduce them here. Just note that the post is based on beta 2 and I’ll be using beta 3.

The bare minimum is to define: a Startup class, where you configure your application. In this case, I’m configuring it to say hello as a response to all the requests.

using System;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;

namespace HelloWorld.Web
{
public class Startup
{
public void Configure(IApplicationBuilder app)
{
app.Run(ctx => ctx.Response.WriteAsync("Hello ASP.NET 5 world! Time: " + DateTime.Now));
}
}
}

And a project.json file with the application dependencies. In this case I’m also configuring a command to be able to run the application from the command line, since I’ll be using a self-host.

{
"dependencies": {
"Microsoft.AspNet.Hosting": "1.0.0-beta3",
"Microsoft.AspNet.Server.WebListener": "1.0.0-beta3"
},
"commands": {
"web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5000"
}
}

The bootstrap is convention-based: ASP.NET searches the main assembly of the application for a class named Startup and then for a method named Configure that has the signature above. The app.Run method is used to register code that kicks in when no other component handles the request. Since I have nothing else, it will run for every request. This is somewhat similar to an IHttpHandler on previous versions.

The web command is an easy way to configure the self-hosted server and then start it using k on the command line. So, if you put the two files above on a folder, you can restore dependencies and run the application from the command line by doing:

kpm restore
k web

You should see a “Started” message on the console and then you can access the application on http://localhost:5000. And that’s it!

On the following post I’ll add a few more bits on this example, namely registering a middleware and hosting on IIS express, as well as looking a bit more into the HTTP abstractions and comparing them to the ones previously found on Katana project.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s