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.
- From the "MVC4" window 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.
- 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.
Now execute the application. The application will use port "8384".
- Now add a Model class as in the following:
- In the "Solution Explorer".
- Right-click on the "Model" -> "Add" -> "Class".
- Select "Installed" -> "Visual C#".
- 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; }
}
}
- Now in the ValuesController perform some changes:
-
In the "Solution Explorer".
-
Expand the "Controller" folder.
-
Select the "ValuesController".
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.