KISS REST Service with Web Api 2.0 / MVC6

web api 2.0

Web API / MVC6

Web API is a way that Microsoft wants to address creating services in ASP.NET MVC. Let’s try how it can be used…

Web API has become a part of MVC6 and actually this is what I’ll be using in this blog post.

WCF Is Dead and Web API Is Dying – Long Live MVC 6!

BTW

Did I tell you that this has been developed on Ubuntu? You don’t believe me? Take a look on previous posts: KISS ASP.NET on Ubuntu.

Yeoman & Generators… to do or not to do?

If you haven’t heard about yeoman, google it. It’s awesome tool that helps you to start with almost any type of framework. However it has a drawback. It will do some stuff and I’m lazy, I’ll never try to understand what is inside. I have reviewed the code which is produced by yo. It’s generally cool, in example it adds logging. But what is actually required?

That’s why, let’s try to write KISS REST Service with Web API 2.0.

KISS Web API 2.0 MVC 6

Project.json

The only thing that you really need is adding MVC dependency to your project:

  "dependencies": {    
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-*",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final"      
  },

If you have tried to run your KISS project on Ubuntu from previous posts: KISS ASP.NET on Ubuntu, you should have Kestrel already.

Startup.cs

Configure.cs

This method is configuration for Dependency Injection in ASP.NET. We can configure there anything including e.g. ORMs.  For KISS approach we need only MVC.

        
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseMvc();
        }

Generators add some stuff there, but they are actually not required.  The only things that you need to have is to register MVC.

What these methods do? Actually it’s hard to discover, there is nothing concrete nowhere. However very shortly:

  • AddMVC()

It will register everything that you need to have.

  • UseMVC()

It will configure your app to use what you have registererd.

Shortly, we need them both even if we follow KISS.

Controllers/UserController.cs

using System.Collections.Generic;
using Microsoft.AspNet.Mvc;


namespace shout.Controllers
{
    [Route("api/[controller]")]
    public class UserController : Controller
    {
        // GET: api/user
        [HttpGet]
        public IEnumerable Get()
        {
            return new string[] { "value1", "value2" };
        }
    }
}

This is our KISS REST service. UseMVC method in service configuration will register this class to be used and Get method will be called by default when reaching out to api/user endpoint.

Starting

$dnu restore
...
$dnu build
...
$dnx kestrel
Hosting environment: Production
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.

Zaznaczenie_003

Code

Shout… at the moment it’s on master branch.

 

3 thoughts on “KISS REST Service with Web Api 2.0 / MVC6

Leave a Reply

Your email address will not be published. Required fields are marked *