1

The issue is that this code gives inner exception and when it goes to the url (api/brand) it gives this error:

The requested resource does not support http method 'GET'.

I have tried many solutions but non are working.


public class BrandController : ApiController
{
    public List<Brands> DetailsGet()
    {
        EFDBFirstDatabaseEntities db = new EFDBFirstDatabaseEntities();
        try
        {
            db.Configuration.LazyLoadingEnabled = false;

            List<Brands> brand = db.Brands.ToList();
            return brand;
        }
        catch (Exception ex)
        {
            if (ex.InnerException != null)
            {
                throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex.InnerException.Message));
            }
            else
            {
                throw ex;
            }
        }        
    }
}

This is model class code:

namespace EntityPractice.Models
{
    using System;
    using System.Collections.Generic;
    
    public partial class Brands
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Brands()
        {
            this.Products = new HashSet<Products>();
        }
    
        public long BrandID { get; set; }
        public string BrandName { get; set; }
    
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Products> Products { get; set; }
    }
}
Jackdaw
  • 7,626
  • 5
  • 15
  • 33
Tariq Ziad
  • 13
  • 6
  • See the following post: [The requested resource does not support HTTP method 'GET'](https://stackoverflow.com/q/12765636/6630084) – Jackdaw Feb 24 '21 at 11:19

1 Answers1

0

Look at the following post: Exception Handling in ASP.NET Web API

It is necessary take care to handle any exceptions raised in Web API methods depend on your method functionality.

In example above instead of throw ex; it is necessary to construct and return some instance of HttpResponseException() too.

Jackdaw
  • 7,626
  • 5
  • 15
  • 33