0

I have an ASP.net Web Application with multiple projects:

  1. WebApp (Main website with aspx pages)
  2. Domain (business logic and data access layer)
  3. Api (webapi project)

I have added reference of Domain in API project, so that i can access the business logic and data layers in API.

Here's my API Class:

public class ShoppingController : ApiController
{
        // GET api/shopping/GetShoppingCartItemsCount
        public int GetShoppingCartItemsCount()
        {
            ShoppingCartDetails shopping = new ShoppingCartDetails();
            var a = shopping.GetShoppingCart();
            if (a != null && a.Count > 0)
                return a.Count;
            return 0;
        }
}

now when i access the above function from my localmachine, localhost/api/shopping/GetShoppingCartItemsCount it throws resource not found error

can anyone tell me how should i access the above WebAPI from browser?

Abbas
  • 4,948
  • 31
  • 95
  • 161

1 Answers1

0

Run multiple project together from following link Running two projects at once in Visual Studio Then

$.ajax({  
          url: 'http://localhost:3413/api/shopping/GetShoppingCartItemsCount', //your api url  
          type: 'GET',  
          dataType: 'json',  
          success: function (data, textStatus, xhr) {  
            console.log(data);  
             },  
                 error: function (xhr, textStatus, errorThrown) {  
                     console.log('Error in Operation');  
                 }  
             });  
Rabby Hasan
  • 356
  • 4
  • 10
  • This solution isn't working for me, i enabled my WebApp and API projects as startup, now i am using the jquery function with api localhost/api/shopping/GetShoppingCartItemsCount but the api url does not gets resolved, also i tried accessing the api url separately in browser, and it throws the resource not found error as earlier. – Abbas Sep 25 '18 at 10:45
  • would you please add attribute into your api method [HttpGet] then try to access. – Rabby Hasan Sep 25 '18 at 10:59
  • Even that's not working, actually i have hosted my whole web application in IIS, and converted the WebApp and API project as Application, now i am accessing the API functions via localhost/api/shopping/GetShoppingCartItemsCount, and here i am getting resource not found error. – Abbas Sep 25 '18 at 11:38
  • Check IIS directory has proper permission to access. – Rabby Hasan Sep 25 '18 at 12:35