Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hI all
I am new to asp.net web api 2
I created api to my controllers/api and populated that with following (1*)
I want to test the api with postman on chrome
but this give me 404 error
this is my webapconfig (2*)
and this is the url that I used to postman : localhost:2383/api/products
and this is the error on postman(3*)
help me if you can
thanks.

What I have tried:

1*

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Mvc;
using My_World.Models;

namespace My_World.Controllers.Api
{
    public class ProductsController : ApiController
    {
        Product[] products = new Product[] 
        { 
            new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 }, 
            new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M }, 
            new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M } 
        };

        public IEnumerable<Product> GetAllProducts()
        {
            return products;
        }

        
        public IHttpActionResult GetProduct(int id)
        {
            var product = products.FirstOrDefault((p) => p.Id == id);
            if (product == null)
            {
                return NotFound();
            }
            return Ok(product);
        }
    }
}


2*
C#
<using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace My_World
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

3*
[HttpException]: The controller for path '/api/products' was not found or does not implement IController.
at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
Posted
Updated 20-Sep-16 7:46am
Comments
Richard Deeming 30-Aug-16 14:50pm    
Did you remember to update your Application_Start in Global.asax.cs to call the API registration code?

GlobalConfiguration.Configure(WebApiConfig.Register);

1 solution

Your URL you are using to access your web api is incorrect.

Your error says you used /api/products. With Web API, I believe unless you specify your action in your api controller as GET you have to use the action name in your URL.

In your case, the URL you need to use is /api/products/GetAllProducts or /api/products/GetProduct/1 are your URL options.

If you were to do

C#
public IEnumerable<Product> Get()
        {
            return products;
        }


Then you could do /api/products and if you did

C#
public IEnumerable<Product> Get(int id)
        {
            return products.Where(m=>m.Id == id);
        }


You could use /api/products/1.

Your other option is to use routes to rename your URL's to however you want. You can declare this routes via attributes or in your RouteConfig.cs file.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900