user3126215
user3126215

Reputation: 13

How to fix http error 500 regarding routing?

I am using asp.net web api 2 and the request is unable to locate and run IHttpActionResult methods. I believe that their is an issue with my routing. The error (http 500) response I get is: "an error occurred when trying to create a controller of type Make sure that the controller has a parameterless public constructor."

The request I send is: Verb: GET localhost:xxxx/api/simpleproduct/getproduct?id=3

[RoutePrefix("api/simpleproduct")]
    public class SimpleProductController : ApiController
    {
        List<Product> products = new List<Product>() { new Product { Id = 1, Name = "Demo1", Price = 1 }, new Product { Id = 2, Name = "Demo2", Price = 2 }, new Product { Id = 3, Name = "Demo3", Price = 3 } };

        public SimpleProductController(List<Product> products)
        {
            this.products = products;
        }

        public IEnumerable<Product> GetAllProducts()
        {
            return products;
        }
        [Route("getproduct")]
        [HttpGet]
        public IHttpActionResult GetProduct(int id)
        {
            var product = products.FirstOrDefault((p) => p.Id == id);
            if (product == null)
            {
                return NotFound();
            }
            return Ok(product);
        }

        public IHttpActionResult AddProduct(int Id, string Name, decimal Price)
        {
            if (Id == 0 || Name == null || Price == 0)
            {
                return Conflict();
            }

            products.Add(new Product { Id = Id, Name = Name, Price = Price });

            return Ok(products);
        }

        public IHttpActionResult DeleteProduct(int Id)
        {
            if (Id < 0)
            {
                return Conflict();
            }

            var products2 = products.ToList();
            foreach (var product2 in products2)
            {
                if (product2.Id == Id)
                {
                    products.Remove(product2);
                }
            }

            return Ok(products);
        }
    }

Upvotes: 0

Views: 1195

Answers (1)

Rei Mavronicolas
Rei Mavronicolas

Reputation: 1435

Like @PmanAce already mentioned in the comments, you need a parameterless public constructor.

public SimpleProductController()
{
}

I assume you're just testing/trying out Web API, which is why you're hardcoding your products as a private variable. In that case you don't need the constructor to take the products parameter at all.

If you get you're example working, you might want to try injecting a repository into your controller. It separates the logic that retrieves the data (that maps to your entity) from the controller. Here's a good article on Dependency Injection for Web API controllers that utilises a repository and Unity to inject it as a dependency. It strangely enough uses the same Product structure you're using.

http://www.asp.net/web-api/overview/extensibility/using-the-web-api-dependency-resolver

Upvotes: 0

Related Questions