Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am executing the following code to get data from Web API:

$(window).load(function () {
           var jsonObject = {
               "CompanyID": 1,
               "UserName": '30',
               "UserPwd": '30',
               "UserRole": 3,
               "RemoteHostIP": '8.8.8.8'
           };
           //debugger;
           $.ajax({
               type: "POST"
                , url: "http://localhost/ESSWebAPI/api/mobi/Login_User",
               data: JSON.stringify(jsonObject),
               contentType: "application/json;charset=utf-8",
               dataType: "json"
                        , success: function (data) {
                            console.log(data);
                        },
               error: function (response) {
                   alert(response.status + " " + response.statusText);
               }
           });
       });


I am getting the following error:

A null value was returned where an instance of HttpResponseMessage was expected.

System.InvalidOperationException

at System.Web.Http.Controllers.ResponseMessageResultConverter.Convert(HttpControllerContext controllerContext, Object actionResult) at System.Web.Http.Controllers.ApiControllerActionInvoker.<invokeactionasynccore>d__0.MoveNext()--- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ActionFilterResult.<executeasync>d__2.MoveNext()--- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<sendasync>d__1.MoveNext()

at System.Web.Http.Controllers.ResponseMessageResultConverter.Convert(HttpControllerContext controllerContext, Object actionResult) at System.Web.Http.Controllers.ApiControllerActionInvoker.<invokeactionasynccore>d__0.MoveNext()--- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ActionFilterResult.<executeasync>d__2.MoveNext()--- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Dispatcher.HttpControllerDispatcher.<sendasync>d__1.MoveNext()


Code in my Web API

C#
[HttpPost]
       //[Route("api/Mobi/Login_User")]
       public HttpResponseMessage Login_User(Login obj)
       {
           try
           {
               tlbset.ESS.Login oLogin = new tlbset.ESS.Login();
               oLogin.UserName = obj.UserName;
               oLogin.UserPwd = obj.UserPwd;
               oLogin.CompanyID = obj.CompanyID;
               oLogin.UserRole = Convert.ToInt16(obj.UserRole);
               oLogin.RemoteHostIP = obj.RemoteHostIP;
               //RemoteHostName
               try
               {
                   System.Net.IPHostEntry host = default(System.Net.IPHostEntry);
                   host = System.Net.Dns.GetHostEntry(oLogin.RemoteHostIP);
                   oLogin.RemoteHostName = host.HostName;
               }
               catch
               {
               }

               List<Result> lstLogin = new List<Result>();

               DataSet dsResult = new DataSet("dsResult");
               DataTable dtLogin = new DataTable("dtLogin");
               DataTable dtMenu = new DataTable("dtMenu");

               dsResult.Tables.Add(dtLogin);
               dsResult.Tables.Add(dtMenu);

               if (oLogin.LoginUser())
               {
                   if (oLogin.EmpCngPwd == 0)
                   {
                       //VALID EMPLOYEE
                       dtLogin.Rows.Add(1, CT.Cryptography.Crypto.Encryption(oLogin.SessionID.ToString()));
                   }
                   else if (oLogin.EmpCngPwd == 1)
                   {
                       //PASSWORD EXPIRE
                       dtLogin.Rows.Add(3, "Your Password is expired");
                   }
                   else if (oLogin.EmpCngPwd == 2)
                   {
                       //CHANGE PASSWORD ON LOG ON
                       dtLogin.Rows.Add(3, "You must set password");
                   }
               }
               else
               {
                   //INVALID EMPLOYEE
                   dtLogin.Rows.Add(2, oLogin.ErrorMsg);
               }

               //MENU
               dtMenu = Get_Menu_Table();
               DataView dv;
               if (!oLogin.objEmployee.ReportingHead)
               {
                   //Pending Approvals
                   dv = dtMenu.DefaultView;
                   dv.RowFilter = "SNo<7 AND SNo>7.3";
                   dtMenu = dv.ToTable();

                   //Team Leave calendar
                   dv = dtMenu.DefaultView;
                   dv.RowFilter = "SNo<>9";
                   dtMenu = dv.ToTable();
               }

               //Employee Directory
               if (!oLogin.objSettings.ShowEmpDir)
               {
                   dv = dtMenu.DefaultView;
                   dv.RowFilter = "SNo<>10";
                   dtMenu = dv.ToTable();
               }

               //Web Punch
               if(!(oLogin.objSettings.ApplyMobilePunch && oLogin.objSettings.InclInMobilePunch))
               {
                   dv = dtMenu.DefaultView;
                   dv.RowFilter = "SNo<>11";
                   dtMenu = dv.ToTable();
               }

               //Payroll
               string sEmpPRModules = "";
               tlbset.ESS.Settings oSettings = new tlbset.ESS.Settings();
               if (oSettings.GetRecord())
                   sEmpPRModules = oSettings.EmpPRModules;
               if (sEmpPRModules != "")
               {//Salary Details
                   if (sEmpPRModules.Contains("1^1|") == false)
                   {
                       dv = dtMenu.DefaultView;
                       dv.RowFilter = "SNo<>8.1";
                       dtMenu = dv.ToTable();
                   }

                   //Salary register
                   if (sEmpPRModules.Contains("3^1|") == false)
                   {
                       dv = dtMenu.DefaultView;
                       dv.RowFilter = "SNo<>8.2";
                       dtMenu = dv.ToTable();
                   }

                   //if none of them is applicable
                   if (!sEmpPRModules.Contains("1^1|") && !sEmpPRModules.Contains("3^1|"))
                   {
                       dv = dtMenu.DefaultView;
                       dv.RowFilter = "SNo<8 AND SNo>8.2";
                       dtMenu = dv.ToTable();
                   }
               }
               return Request.CreateResponse(HttpStatusCode.OK, dsResult);
           }
           catch (Exception ex)
           {
               CTException.WriteDBLog(strCodeFile, "Login_User", ex.Message, ex.InnerException);
               return null;
           }
       }


What I have tried:

I have tried changing post method to GEt. But it doesnt work
Posted
Updated 6-Aug-18 6:09am
v2
Comments
F-ES Sitecore 3-Aug-18 9:22am    
The error is probably happening in your c# code so I'd start looking there.
prapti.n3 4-Aug-18 1:20am    
I have added the code in Web API. Please check

1 solution

Your API code is throwing an exception. When that happens, your action returns null rather than an appropriate response message.

Change your catch block to return a suitable response message:
C#
catch (Exception ex)
{
    CTException.WriteDBLog(strCodeFile, "Login_User", ex.Message, ex.InnerException);
    return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex);
}

You will then need to check your log file to find out what the exception is, and fix your code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900