Datatables AJAX request not hitting Web API correctly

Datatables AJAX request not hitting Web API correctly

shoebshoeb Posts: 1Questions: 1Answers: 0

I am trying to integrate ASP.NET Web API 2 with jQuery Datatables 1.10.7.

I want to use Server Side processing in my Datatables, so I am used the Nuget package DataTables.AspNet.WebApi2

Here is my javascript

<script>
    $(document).ready(function () {
        $('#example').dataTable({
            "serverSide": true,
            "ajax": "/api/StudentsBasicInfo/GetPaginatedStudentsList2",
            columns: [
            {
                name: 'FirstName',
                data: 'FirstName',
                title: "First Name",
                sortable: false,
                searchable: false
            },
            {
                name: 'LastName',
                data: "LastName",
                title: "Last Name",
                sortable: false,
                searchable: false
            }
            ]
        });
    });
</script>

Here is my HTML

<table id="example" class="display" cellspacing="0" width="100%">
</table>

Here is my Web API

public JsonResult<IDataTablesResponse> GetPaginatedStudentsList2(IDataTablesRequest request)
    {
        StudentsInfoHybrid searchObj = new StudentsInfoHybrid();

        StudentsBasicInfoBL blClass = new StudentsBasicInfoBL();

        try
        {
            searchObj.PageSize = request.Length;
            searchObj.Offset = request.Start;
            searchObj.count = false;

            //get the list of students
            var students = blClass.GetAllStudentsHybridInfo(searchObj);

            //filter list according to the criteria passed in the request object
            var filteredStudents = students.Where(i => i.FirstName.Contains(request.Search.Value)).ToList();

            //reset page size and offset so that count of total objects can be obtained
            searchObj.PageSize = 0;
            searchObj.Offset = 0;

            var totalCount = blClass.CountAllStudentsHybridInfo(searchObj);

            var response = DataTablesResponse.Create(request, students.Count(), filteredStudents.Count(), filteredStudents);

            return new DataTablesJsonResult(response, Request);

        }
        catch (Exception ex)
        {
            return null;
        }
        finally
        { }
    }

My code builds perfectly, but during execution when datatable calls the Web API, I can see in Debug mode that the IDataTablesRequest request object is null.

I have attached a screenshot of Google Chrome network log.

As I can see from the Network Log, the Web API is returning this exception which I have no clue about.

{"Message":"An error has occurred.","ExceptionMessage":"A null value was returned where an instance of IHttpActionResult was expected."

I am also attaching another screenshot of what my Request looks like from Google Chrome's network log.

Answers

This discussion has been closed.