Introduction
This article explains how to call the ASP.NET Web API in same domain using AJAX in asimple and step by step way. We can deploy and use our Web API in the same domain as well as cross-domain.
Calling Web API in same domain
Web page and Web API should be hosted in the same domain or same origin. For example, if a web application is hosted on “http://test.com”, then, the Web API should also be hosted on “http://test.com”.

Steps for creating same domain
Steps 1
Open a new application and add Web API Solution. After opening ASP.NET Web API solution, add new Web API Controller.

Now, add a class in a Models folder for accessing data from Web API Controller to the web page. The added class and its properties look like the below code.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace WebAPIsameDomain.Models
- {
- public class Customer
- {
- public int Id { set; get; }
- public string CustomerName { set; get; }
- public string Address { set; get; }
- public string City { set; get; }
- public string Pincode { set; get; }
- }
- }
Step 2
Assign customer details in customer list variable and add in “DemoController”.
- namespace WebAPIsameDomain.Controllers
- {
- public class DemoController : ApiController
- {
- public IEnumerable<Customer> Get()
- {
- return new List<Customer>
- {
- new Customer{Id=1,CustomerName="Arun",Address="9th Street",City="Erode",Pincode="637204"},
- new Customer{Id=2,CustomerName="Mohan",Address="Main Road",City="Namakkal",Pincode="637205"},
- new Customer{Id=3,CustomerName="Prapbu",Address="SKV Street",City="Salem",Pincode="637206"},
- new Customer{Id=4,CustomerName="Raja",Address="Raja Street",City="Chennai",Pincode="637207"},
- new Customer{Id=5,CustomerName="Ravi",Address="SMV Street",City="Covai",Pincode="637208"},
- new Customer{Id=6,CustomerName="Santhose",Address="SKM Street",City="Salem",Pincode="637209"},
- new Customer{Id=7,CustomerName="Mugesh",Address="Main Road",City="Trichy",Pincode="637846"},
- new Customer{Id=8,CustomerName="Mani",Address="Mani Street",City="Chennai",Pincode="637245"},
- new Customer{Id=9,CustomerName="Sankar",Address="Sankar Street",City="Tiruchengode",Pincode="637273"},
- new Customer{Id=10,CustomerName="Vignesh",Address="Main Road",City="Kondankattur",Pincode="637201"}
- };
- }
- }
- }






chirag bhattPosted Nov 10, 2017, 4:32 PM
Very well documented. Thanks for sharing.
Sankar BcPosted Feb 22, 2017, 10:18 AM
Good article Vignesh
Vishal PrajapatiPosted Feb 21, 2017, 3:07 AM
Worth Reading.....
om kumarPosted Feb 21, 2017, 12:42 AM
Thanks for sharing!
Anu VPosted Feb 20, 2017, 11:49 PM
Nice article.. Thanks for sharing..
Sahil SharmaPosted Feb 20, 2017, 11:33 PM
Thanks for sharing