Hello ASP.NET 5 World – Part III

In the previous post I improved the hello world example to include the static files middleware. In this post I’ll wrap up this series by adding MVC. The first thing is to add the corresponding package. This can be done on the command line:

kpm install Microsoft.AspNet.Mvc 6.0.0-beta3

As the static file middleware, MCV includes a convenience extension method to add itself to the pipeline. We can use that method when configuring the application:

public void Configure(IApplicationBuilder app)

In addition, since dependency injection is now used throughout the framework, we need to register the services required by MVC on the DI container. I’m not yet familiar with the details of the DI model on ASP.NET 5, but there’s another method (besides Configure) that the framework invokes by convention when starting up an application:

public void ConfigureServices(IServiceCollection services)

Note the usage of AddMvc, an extension method also included in MVC to register its services on the container.

MVC 6 Includes the features that were previously found on both MVC and Web API. Everything should be familiar, having in mind that the base HTTP programming model is a bit different. That said, adding a controller is straightforward. Just add a class whose name has the Controller suffix, define an action and its route and your good to go. You don’t need to extend the base Controller class, even though it has many useful methods and properties (View(), Redirect(), Request, Response).

public class HelloWorldController
public object Index()
return new
Message = "Hello ASP.NET 5 world!",
Time = DateTime.Now.ToString(),

If we run the app from the console an navigate to http://localhost:5000/hello we’ll get a JSON response returned by the controller. The same controller can also have an action returning an IActionResult which corresponds to a view:

public IActionResult Goodbye()
return View();

And that’s it for now!


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 )

Facebook photo

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

Connecting to %s