By   August 28, 2016

Creating Web APIs with ASPNet Core
We have seen how we can setup our mac environment for .net based development. Here we discussed how we created a sample application using Visual Studio and Dotnet CLI tools.

http://www.alternatestack.com/development/developing-c-applications-on-mac-os-using-vs-code/

In this post we are going to see how we can use command line tools to create our first Web API using ASP.NET Core. Here we are going to use Yeoman for code generation and DOTNET CLI to build and run it. We have discussed Yeoman before for building AngularJs applications [http://www.alternatestack.com/development/rest-service-in-angular-js/].

After setting up your machine for .net core development, let’s first install asp.net generator for yeoman using npm.

Now we can just use this generator to create a web api project using yo aspnet command. The generator gives an option to select the project type. Here we have selected Web API Application. Let’s name our API as StudentService.

yo-aspnet

yo-aspnet

As we remember, the first thing we do after generating a project is to restore all the dependencies. Just run dotnet restore and it would restore all dependencies for you.

dotnet restore

dotnet restore

The most amazing thing about asp.net core project is its reduced project contents. I have always been amazed by the number of folders and their contents just to host a simple Web API. Just compare it to some golang or javascript node based service. Here is the project structure.

aspnet core folders

aspnet core folders

A yeoman generator can also have sub-generators. Let’s explore these sub-generators using yo aspnet –help command. Here is the list:

aspnet sub-generators

aspnet sub-generators

Let’s use WebApiController sub-generator to generate StudentsController. Here we are naming our controller as StudentsController. It would be generated in Controllers folder by default.

WebApiController

WebApiController

Now let’s use Class sub-generator to create Student class. This would be used to describe a Student entity. We are creating it in model folder.

Class Sub-Generator

Class Sub-Generator

Now let’s build and run the service using dotnet build and dotnet run commands.

Let’s hit it using Postman. Here we we have the response for GET request from the service.

student-service-get

student-service-get

If this is the first time you are developing a .net core project, and you are curious like a cat then you must be wondering where the packages folder is. Actually, this has been optimized for the framework. Now we just have one packages folder for the user and all applications use the same folder. This reduces the disk usage on a developer’s machine. The packages are in .nuget\packages folder. The default nuget configuration is in .nuget\Nuget.config file.

packages .nuget

packages .nuget

You can always create a local Nuget.Config file for a different nuget repository to be used for a project.

Code Repository

code repository

code repository