locked
Entity framework and web API error 500 RRS feed

  • Question

  • User632428103 posted

    Hello all,

    at the moment, i try to get understand how works asp net API ..

    i've follow this sample locate here :

    http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

    My project works fine when datas are in the controller as this :

     public class ProductSampleController : ApiController
        {
            //
            ProductTest[] myProducts = new ProductTest[]
            {
                //
                new ProductTest { Id=1, Name="produit 1", Category = "Groceries", Price=1},
                new ProductTest { Id=2, Name="produit 2", Category="Toys", Price=3.75M},
                new ProductTest { Id=3, Name="produit 3", Category="Harware", Price=16.99M},
                new ProductTest { Id=4, Name="produit 4", Category="Groceries", Price=16.99M},
                new ProductTest { Id=5, Name="produit 5", Category="Toys", Price=16.99M},
                new ProductTest { Id=6, Name="produit 6", Category="Computer", Price=16.99M},
                new ProductTest { Id=7, Name="produit 7", Category="Others", Price=16.99M},
                new ProductTest { Id=8, Name="produit 8", Category="Bike", Price=16.99M},
                new ProductTest { Id=9, Name="produit 9", Category="Car", Price=16.99M}
            };
    
            /// <summary>
            /// GetAll
            /// </summary>
            /// <returns></returns>
            public IEnumerable<ProductTest> GetAll()
            {
                return myProducts;
            }
    }

    the method is call on javascript as this :

     var apiURL = 'api/Client/';
           $(document).ready(function () {
               //
               $.getJSON(apiURL)
                      .done(function (data) {
                 
             //alert('done');
                 
                          var datas = '';
                          $.each(data, function (key, item) {
                              datas += formatItem(item);
                          });
    
                          $('#list').append(datas);
                      }).fail(function (jqXHR, textStatus, err) {
                          alert(err + ' ' + textStatus + ' ' + jqXHR);
                      });
           });
    
      //-----------------------------------------------
           // formatItem
           //-----------------------------------------------
           function formatItem(item) {
               return item.Id + ' <br /> ';
           }

    Now, i've decide to migrate this sample to entites framework but every time when i call the method i receive and error 500 internal server error ?!

    I don't hnow how ?

    Any idea ?

    Here is it my code => my entites is base on NORTHWIND database ..

      public IEnumerable<Customer> Get()
            {
                //
                using (NORTHWINDEntities entites = new NORTHWINDEntities())
                {
                    return entites.Customers;
                }
            }

    and the call client :

     var apiURL = 'api/Client/';
    
      
           $(document).ready(function () {
    
               $.getJSON(apiURL)
                      .done(function (data) {
                          //alert('done');
                          var datas = '';
                          $.each(data, function (key, item) {
    
                              datas += formatItem(item);
                          });
    
                          $('#list').append(datas);
                      }).fail(function (jqXHR, textStatus, err) {
                          alert(err + ' ' + textStatus + ' ' + jqXHR);
                      });
           });
    
    
           //-----------------------------------------------
           // formatItem
           //-----------------------------------------------
           function formatItem(item) {
               return item.CustomerID + ' <br /> ';
           }

    the call JS is the same , the only difference is the method formatItem ..

    Thanks for your time

    Friday, September 25, 2015 4:20 AM

All replies

  • User1644755831 posted

    Hello Jimmy,

    Try this and put a break point and try catch to see if you are getting and exception message.

     public IEnumerable<Customer> Get()
            {
                //
                using (NORTHWINDEntities entites = new NORTHWINDEntities())
                {
                    return entites.Customers.ToList();
                }
            }

    With Regards,

    Krunal Parekh

    Monday, September 28, 2015 5:42 AM
  • User603616845 posted

    Hi,

    I am agree with @krunal, Please see below your code... your method is IEnumerable<Customer> which will be returning the list of customer. 

    public IEnumerable<Customer> Get()

    So, Here you just need to return Customer list as Krunal suggested to you.

    From that articles you learn this .. there is also returning list..

    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; //this is list of product 
            }

    Hope this will help you understand clear.

    thanks

    Monday, September 28, 2015 5:51 AM
  • User632428103 posted

    Hello all,

    thanks for your time but that  doesn't work ..

     NORTHWINDEntities entites = new NORTHWINDEntities();
    
            //
            [HttpGet]
            public IEnumerable<Customer> GetAll()
            {
                try
                {
                    return entites.Customers.ToList();
     
                }
                catch (Exception ex)
                {
                    string s = ex.Message;
                }
    
                //
                return null;
            }

    nothing in client are happen ..

    But after search on internet if i do this that's work :

            IEnumerable<CustomerTest> myList;
    
            //
            [HttpGet]
            public IEnumerable<CustomerTest> GetAll()
            {
                try
                {
                    myList = from x in entites.Customers
                             select new CustomerTest()
                             {
                                 CustomerID = x.CustomerID,
                                 CompagnyName = x.CompanyName,
                                 ContactTitle = x.ContactTitle,
                                 City = x.City,
                                 Country = x.Country
                             };
    
    
    
                    //
                    return myList.ToList();  
                }
                catch (Exception ex)
                {
                    string s = ex.Message;
                }
    
                //
                return null;
            }

    that's work fine when i pass with an other object who contains information from customer ..

    I don't know how but that's work ..

    Any explanation or solution why entites.customer.toList not work 

    thanks for all guys

    Tuesday, September 29, 2015 6:03 AM
  • User1644755831 posted

    Hello Jimmy,

    Do you have any navigation properties in your Customer project? I would suggest that you use Viewmodel here and map your product class to viewmodel then pass it to the client. If you are calling this using ajax then handle the error function of the $.ajax and see what exception you get.

    With Regards,

    Krunal Parekh

    Tuesday, September 29, 2015 10:02 PM
  • User753101303 posted

    Hi,

    http 500 is a server side error so you should see an exception in the Windows Application log. Once you know what is the exact error you have, it will be much easier to fix.

    Wednesday, September 30, 2015 7:13 AM
  • User632428103 posted

    Hello all,

    @Krunal => i can't understand what you mean with use viewModel ?

    @PatriceSc =>if i look in my computer because i use localhost as my site, if i look in component services (lwindows log) i don't have any information about error with my solution

    any idea ?

    I inform you, i'm work with visual studio 2012 but all package (dll) are installed in my computer.

    Tanks for all

    Have a nice day

    Thursday, October 1, 2015 3:13 AM