Creating A REST Webservice With .NET Core

In this tutorial Creating A REST Webservice With .NET Core I will show you how to create a REST Webservice with .NET Core easy and fast. For this purpose we will need Visual Studio. I will use the very same version as in my other tutorial for .NET Classic. However, since .NET Core doesn’t only rely on windows you can look for any other major platform like MAC OS or Linux to get things up and running.

Create the Project

In the first step we create a .NET Core Project and we call it MyRESTService.


Choose Net Core Project for REST Webservice
Choose Net Core Project

Close ×

Within the second screen you can choose between multiple options, but if you want to go as clean as possible you should opt for the Empty Template. Make also sure that you are on the most current version of .NET Core – in my case it is 2.0.

Choose Emtpy Template and .NET Core 2
Choose Emtpy Template and .NET Core 2

After that confirm your selection with OK and Visual Studio automatically starts creating your project.


Visual Studio Creating Screen
Visual Studio Creating Screen

Close ×

 

Right after the creation process you will see this project in your solution explorer.

Project Overview
Project Overview

 

If we run this Emtpy Template project by pressing F5 or the green Run Icon we see nothing but a Hello World! output in the browser. Technically your project is build and deployed to your local internet information server instance which works as a webserver.

Hello World
Hello World Example

In your Startup.cs you will find something like this snippet which creates the Hello World output.

So far so good, but we want to create a REST Webservice where we can make at least a GET call to retrieve some data. For this purpose select the wwwroot symbol – press right click and chosse Add Controller.

Add Controller
Add Controller to your project

Because we want a very clean project we choose only to add the minimal dependencies to our project in the next dialog. However, if you want to use some of the listed stuff which is included in the full package you can also choose otherwise.

Dependency Dialog
Dependency Dialog

Amend the Startup.cs

In the process of creating a project Visual Studio will create a ScaffoldingReadMe.txt – make sure that you follow the steps there.

This Configuration tells your program to handle requests which are sent to the webserver. Now we have to do the Add Controller action once more to get the dialog where we can choose which Controller type we want to add. I would highly recommend to create a dedicated Controller folder where all of you controllers are together.

Adding custom controllers

In the next dialog we choose the API Controller with read/write actions. I would recommend this to users who just started with all of this just because you get all actions pre-created.

Choose API Controller
Choose API Controller

We choose to name the Controller PersonController. Within the controller folder now the PersonController is created and accessible.

Controller Folder
Controller Folder with PersonController

If you now run the application again you firstly will notice that you will get an 404 – not found error, but don’t worry, that is because we removed the Hello World prior with the updated methods from the ScaffoldingReadMe.txt. Just add /api/person to your url – in my case it looks like http://localhost:59336/api/person – and you will trigger the GET function to receive and array of strings with value1 and value2 as values.

Default Values
Default Values

You can also change the launchUrl which will be accessed after starting your application by updating your launchSettings.json (located in the Properties folder) like the example below.

Close ×

To see if the GET function works as you expected you can simply change the values to see if the results differs. In my test I just renamed the values to names and added a third one.

The result also changed as expected.

New result
New result

So we can assume that everything is working as expected. If you do need – and you probably will – more than one controller just repeat the prior steps.

If you have any questions do not hesitate to use the comment section below and as always – happy coding :).

7 Replies to “Creating A REST Webservice With .NET Core”

  1. Hello Patrick,

    After reading a gazillion of website I have finally run into yours which I found to be A) very well structured and B) the instructions actually work !

    I am new to web development in .NET, could you please give me pointers to documentation/blogs showing how to deploy the RESTful web service to IIS?

    Thank you in advance.

    Cheers
    Omar

    1. Hi Omar, I am glad that it helped you!

      If you are working with Visual Studio you can use the built-in Publish function which does a fantastic job. However, the most deployments to an IIS means to build the Project (With Release Config): and paste the built output into an IIS app directory.

  2. Hi Patrick,
    I just started with .net and I was wondering that Is this solution available as a download somewhere? I’ve only VSCode and I think it doesn’t have these solution helpers. Would help to see whole solution.
    Thanks for good article.

    Br,
    -Janne

    1. Hi, were you able to get a copy of a download for this. I wasnt able to get this to work, and as hoping to see the solution

  3. Hi please post the solution. Following this completely, but still getting localhost not found put in the Custom Controller

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.