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.

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
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

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:
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Web;  
    5. namespace SelectPortWebAPI.Models  
    6. {  
    7.     public class Product  
    8.     {  
    9.         public int Id { getset; }  
    10.         public string Name { getset; }  
    11.         public string category { getset; }  
    12.     }  
    13. }

     

Now in the ValuesController perform some changes:
  • In the "Solution Explorer".
  • Expand the "Controller" folder.
  • Select the "ValuesController".

    Select Controller

Add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Net;  
  5. using System.Net.Http;  
  6. using System.Web.Http;  
  7. using SelectPortWebAPI.Models;  
  8. namespace SelectPortWebAPI.Controllers  
  9. {  
  10.     public class ValuesController : ApiController  
  11.     {  
  12.         // GET api/values  
  13.         public Product Get()  
  14.         {  
  15.             return new Product  
  16.             {  
  17.                 Id = 1,  
  18.                 Name = "Keyboard",  
  19.                 category = "Hardware"  
  20.             };  
  21.         }  
  22.     }  
  23. } 

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


Similar Articles