Access a Web API application from a remote Windows Server 2016 machine

Overview

This is a pretty short article that shows you step by step the way you can create a simple Web API application (ASP .NET) and access it remotely from a Windows Server 2016 machine. This is an ideal, simple, and efficient solution for deploying REST API’s really fast. I haven’t found a clear and coherent article on the web that approaches this subject and I realized that it’s the appropriate moment to make one.

Why Windows Server 2016? Because it’s the only image that can be found on all (or most of) cloud platforms, when you want to create a virtual machine. Only Azure has a Windows 10 image. The rest of them (Google Cloud Platform, AWS and more) have only Windows Server images. However, the setup is similar on both Operating Systems.

I’ll assume that on the VM is installed Visual Studio because the main point of this article is to make the entire setup available in debug mode, so you can see that the requests are really received. In the next article, we’ll show the way we can deploy a Web API application on IIS and also the way we can secure it with SSL.

 

Step 1: Create the Web API app on the VM

This is a pretty simple step. You just have to go on your Windows Server 2016 machine, open Visual Studio, create a blank Web API application, add a simple controller and a simple HTML page so you can see it working when you want to run it. This is the controller code. Just right click on the project and then add a new controller with the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace TestAPI.Controllers
{
    [RoutePrefix("api/test")]
    public class TestController : ApiController
    {

        [HttpGet]
        [Route("test_method")]
        public void TestMethod()
        {


        }
     }
}

As you see, we create a simple method and specify a route to that method (/api/test/test_method). We can update this as we want.

Also make sure that the WebApiConfig file from the App_Start folder is the following one:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;

namespace TestAPI.App_Start
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

This will create a template route for our API so that it should know how to direct the control flow to the right controller / method. It’s one of the most important files that we need to update.

 

Step 2: Set the ports accordingly

Any process that deals with the network, will definitely run on a port. We can adjust that port on our Web API application (by right clicking on the project and going to Web Settings) if we want to use a specific port for our API. Also, even if you don’t want to change it, you have to know what it is. You can see it if you go on the properties of the project and also when you run the app, the link in the browser will specify that port, as follows: http://localhost:<port>.

Why do you need that port? Because right now we need to make port forwarding, which basically means to open a port on the firewall where your machine is stored, and then map that port to the internal port on which our API runs.

If your VM is placed in the cloud, you’ll have to go to the dashboard (or to the control panel) where your VM instance is run and change the network settings accordingly. In general, this is an easy task. You just have to have in mind the fact that you need to bind an external port to an internal port (which is the port of your API).

In order to check if the port forwarding you can analyze this website.

 

Step 3: Make the magic setting

In order to test our connection, you just have to run the API and then you have to use a REST Client (like Postman) to make a GET request to the following endpoint: http://<your_vm_public_ip>:<external_port>:/api/test/test_method

If you test it only with the settings made above, it will NOT work. Why? The port forwarding is set up, the app powered on, why blocks the connection?

The problem is with Visual Studio. We need to tell it to bind the internal port to our Web API. We do that by going in your root solution -> .vs folder -> config -> applicationhost.config.
Open that app and locate the line similar to this one: <binding protocol=“http” bindingInformation=“*:<your_internal_port>:localhost”/>. This line is under <sites> tag

Modify this line, by removing the “localhost” from the end. Save it. Restart VS and it will work now.

I call this setting “magic” because it was a real challenge to find it. There are a lot of variations of this setting (by putting * instead of localhost, etc.) which worked on some people but on all my configurations this setting worked perfectly everytime.

 

Conclusion

Basically, those are all settings that you need to perform in order to get a full blown ASP .NET REST API remotely accesible.

Please contact me for any kind of observation / suggestion.

Have a great day!

Cosmin Ionita

Leave a Reply

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