locked
beginner web api => Get => 500 error ? RRS feed

  • Question

  • User632428103 posted

    Hi all,

    At the moment i read a book about jquery 2  and asp net and i'm trying to do the sample inside this book.

    (book => beginning jquery 2 for asp net developers ..)

    In chapter 8, he talk about restfull service (web api).

    There is a sample with the database northwind with some method (get, update, delete, ..)

    i'm trying to realize the same sample but every time for the get method , i receive an error 500 ..

    Here is it my code, what i wrong ?

    client code :

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>title</title>
    <script src="~/Scripts/jquery-2.0.3.js"></script>
    <script type="text/javascript">
    
    
        $(document).ready(function ()
        {
            var options = {
                url: "/api/Employees",
                type: "GET",
                contentType:"application/json",
                dataType: "json"
            };
    
            //
            $.ajax(options).done(function (employees)
            {
                alert('done');
                //
                //for (var i = 0; i < employees.length; i++)
                //{
                //    $("#ddlEmployeeID").append("<option>" + employees[i].EmployeeID + "</option>");
                //}
            }).fail(function (jqXHR, status, err)
            {
                alert('fail');
                $("#lblMessage").html(err).append(status).append(jqXHR);
            });
        });
    
    </script>
    and so on ....

    and i have an EmployeesController as this :

     public class EmployeesController : ApiController
        {
    
            //
            NORTHWINDEntities db = new NORTHWINDEntities();
    
    
            public List<EmployeeData> GetEmployees()
            {
                //
                var data = from e in db.Employees
                        orderby e.EmployeeID
                        select new EmployeeData {
                        EmployeeID=e.EmployeeID,
                        FirstName=e.FirstName,
                        LastName=e.LastName,
                        BirthDate=e.BirthDate,
                        HireDate = e.HireDate,
                        Country = e.Country};
    
                //
                List<EmployeeData> list = data.ToList();
    
                //
                return list;
            }

    I've search on my friend google about this error and find some solution but none of them resolve my problem ..

    thanks for your time and your knowledge 

    Thursday, October 22, 2015 6:29 AM

All replies

  • User-1923420989 posted
    try adding as "new EmployeeData()"
    
     
    public List<EmployeeData> GetEmployees()
            {
                //
                var data = from e in db.Employees
                        orderby e.EmployeeID
                        select new EmployeeData() {
                        EmployeeID=e.EmployeeID,
                        FirstName=e.FirstName,
                        LastName=e.LastName,
                        BirthDate=e.BirthDate,
                        HireDate = e.HireDate,
                        Country = e.Country};
    
                //
                List<EmployeeData> list = data.ToList();
    
                //
                return list;
            }

    Thursday, October 22, 2015 7:26 AM
  • User-782957977 posted

    Other than GetEmployees(), do you have any other rest  methods in EmployeesController? If so please comment other rest methods and try.  If more than one  method match same Web Api route, you will get this type of error. Also please post routes in WebApiConfig.cs

    Also it would be good to use [HttpGet] attribute as given below

    [HttpGet]
    public List<EmployeeData> GetEmployees()
            {
                 
            }

    Thursday, October 22, 2015 9:27 PM
  • User632428103 posted

    Hello santhoshje and abrly,

    thanks for your time but it's an error from me ...

    On the book, it tell to create a new HomeController with APIController empty ..

    when i do that, the new controller was created but with some method (GET, POST, ...)

    I've continue to add the code inside the controller and of course add a new GET method ...

    That's the problem ..

    for to discover the problem, i've copy paste the code from the book to my project :).

    thanks for your time and have a nice day

    Friday, October 23, 2015 2:48 AM
  • User-219423983 posted

    Hi jimmy69,

    On the book, it tell to create a new HomeController with APIController empty ..

    when i do that, the new controller was created but with some method (GET, POST, ...)

    About adding an empty Web API Controller, you could take a look at the following article. You could right-click the Controllers folder and select Add and then select Controller. In the Add Scaffold dialog, select Web API Controller - Empty.

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

    I hope it’s useful to you.

    Best Regard,

    Weibo Zhang

    Friday, October 23, 2015 3:58 AM