How to Set Specific Page and Port in Web API

Introduction

This article show how to select a specific page and port in the Web API. Here we define the specific port for execution of the application. The application can execute on the defined specific port. Now let's see the example of the application.

  1. Create an Web API Application.
  • Start Visual Studio 2012.
  • From the Start window select "New Project".
  • Then select "Installed" -> "Visual C#" -> "Web".
  • Select "MVC4 Web Application" and click on the "OK" button.

Select MVC4 Application

  • From the "MVC4" window select "Web API".

Select Web API

  1. After creating the application open the property window of the Web API project.
  • In the Solution Explorer.
  • Double-click on the properties.

Select Property window

  • Now open a property window.
  • Select "Web" and select the "Specific Page" Radio Button and type the page.
  • Now select "Use Visual Studio Development Server" and  "Specific port" and enter the port.

Set Property

Now execute the application. The application will use port "8384".

Get value

  1. Now add a Model class as in the following:
  • In the "Solution Explorer".
  • Right-click on the "Model" -> "Add" -> "Class".
  • Select "Installed" -> "Visual C#".

Add Model Class

  • Select "Class" and click on the "Add" button.

Add the following code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

 

namespace SelectPortWebAPI.Models

{

    public class Product

    {

        public int Id { get; set; }

        public string Name { get; set; }

        public string category { get; set; }

    }

}

  1. Now in the ValuesController perform some changes:
  • In the "Solution Explorer".

  • Expand the "Controller" folder.

  • Select the "ValuesController".

Select Controller

Add the following code:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Net;

using System.Net.Http;

using System.Web.Http;

using SelectPortWebAPI.Models;

namespace SelectPortWebAPI.Controllers

{

    public class ValuesController : ApiController

    {

        // GET api/values

        public Product Get()

        {

            return new Product

            {

                Id = 1,

                Name = "Keyboard",

                category = "Hardware"

            };

        }

    }

}

Now again execute the application. it uses the port "8384" and the "api/values" page. It can not use the other port.

Get All Value

Up Next
    Ebook Download
    View all
    Learn
    View all