Там, где ожидался экземпляр httpresponsemessage, возвращалось нулевое значение.
Я выполняю следующий код, чтобы получить данные из веб-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); } }); });
Я получаю следующую ошибку:
Там, где ожидался экземпляр HttpResponseMessage, возвращалось нулевое значение.
Система.Исключение 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()
Код в моем веб-API
[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; } }
Что я уже пробовал:
Я попробовал изменить метод post, чтобы получить. Но это не работает
F-ES Sitecore
Ошибка, вероятно, происходит в вашем коде c#, так что я бы начал искать там.
prapti.n3
Я добавил код в Web API. Пожалуйста, проверьте