返回null值,其中需要httpresponsemessage的实例。 [英] A null value was returned where an instance of httpresponsemessage was expected.
问题描述
我正在执行以下代码以从Web API获取数据:
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);
}
});
});
我收到以下错误:
返回一个空值,其中预期有一个HttpResponseMessage实例。
System.InvalidOperationException
at System System.Web.Http.Controllers.ApiControllerActionInvoker中的.Web.Http.Controllers.ResponseMessageResultConverter.Convert(HttpControllerContext controllerContext,Object actionResult)。< invokeactionasynccore> d__0.MoveNext()---来自先前位置的堆栈跟踪结束,其中异常是抛出---位于System.Web.Http.Controllers.ActionFilterResult的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)。< executeasync> d__2.MoveNext ()---在抛出异常的前一个位置的堆栈跟踪结束---在Syst的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) System.Web.Http.Dispatcher.HttpControllerDispatcher上的em.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)。< sendasync> d__1.MoveNext()
at System.Web.Http.Conttlers.ApiControllerActionInvoker中的System.Web.Http.Controllers.ResponseMessageResultConverter.Convert(HttpControllerContext controllerContext,Object actionResult)。< invokeactionasynccore> d__0.MoveNext()---来自先前位置的堆栈跟踪结束在System.Web.Http.Controllers.ActionFilterResult的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)中抛出了它。< executeasync> d__2。 MoveNext()---从抛出异常的先前位置开始的堆栈跟踪结束---在System.Runtime.CompilerServices.TaskAwaiter.HandleNon上的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)处System.Web.Http.Dispatcher.HttpControllerDispatcher上的SuccessAndDebuggerNotification(任务任务)。< sendasync> d__1.MoveNext()
代码在我的Web API中
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
[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方法更改为GEt。但它不起作用
What I have tried:
I have tried changing post method to GEt. But it doesnt work
推荐答案
(窗口).load(function(){
var jsonObject = {
CompanyID:1,
UserName:'30',
UserPwd:'30',
UserRole:3,
RemoteHostIP:'8.8.8.8'
};
//调试器;
(window).load(function () { var jsonObject = { "CompanyID": 1, "UserName": '30', "UserPwd": '30', "UserRole": 3, "RemoteHostIP": '8.8.8.8' }; //debugger;
.ajax({
类型:POST
,url:http:// localhost / ESSWebAPI / api / mobi / Login_User,
data:JSON.stringify(jsonObject),
contentType:application / json; charset = utf-8,
dataType:json
,成功:函数(数据){
console.log(数据);
},
错误:函数(响应){
alert(response.status ++ response.statusText);
}
});
});
.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实例。
System.InvalidOperationException
at System System.Web.Http.Controllers.ApiControllerActionInvoker中的.Web.Http.Controllers.ResponseMessageResultConverter.Convert(HttpControllerContext controllerContext,Object actionResult)。< invokeactionasynccore> d__0.MoveNext()---来自先前位置的堆栈跟踪结束,其中异常是抛出---位于System.Web.Http.Controllers.ActionFilterResult的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)。< executeasync> d__2.MoveNext ()---在抛出异常的前一个位置的堆栈跟踪结束---在Syst的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) System.Web.Http.Dispatcher.HttpControllerDispatcher上的em.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)。< sendasync> d__1.MoveNext()
at System.Web.Http.Conttlers.ApiControllerActionInvoker中的System.Web.Http.Controllers.ResponseMessageResultConverter.Convert(HttpControllerContext controllerContext,Object actionResult)。< invokeactionasynccore> d__0.MoveNext()---来自先前位置的堆栈跟踪结束在System.Web.Http.Controllers.ActionFilterResult的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)中抛出了它。< executeasync> d__2。 MoveNext()---从抛出异常的先前位置开始的堆栈跟踪结束---在System.Runtime.CompilerServices.TaskAwaiter.HandleNon上的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)处System.Web.Http.Dispatcher.HttpControllerDispatcher上的SuccessAndDebuggerNotification(任务任务)。< sendasync> d__1.MoveNext()
代码在我的Web API中
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
[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方法更改为GEt。但它不起作用
What I have tried:
I have tried changing post method to GEt. But it doesnt work
您的API代码抛出异常。当发生这种情况时,您的操作将返回null
而不是相应的响应消息。
更改catch
阻止返回合适的响应消息:
Your API code is throwing an exception. When that happens, your action returnsnull
rather than an appropriate response message.
Change yourcatch
block to return a suitable response message:
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.
这篇关于返回null值,其中需要httpresponsemessage的实例。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!