C# 在MVC控制器中使用会话返回错误
我正在尝试使用C# 在MVC控制器中使用会话返回错误,c#,asp.net-mvc,session,asp.net-web-api,C#,Asp.net Mvc,Session,Asp.net Web Api,我正在尝试使用会话,但我通常使用的方式不起作用 [HttpPost] [Route("FiltroSiltDet")] public HttpResponseMessage FiltroSiltDet(string DT_INCLUSAO) { try { HttpSessionState Session = HttpContext.Current.Session; Session["DT_INCLUSAO"] = "test";
会话
,但我通常使用的方式不起作用
[HttpPost]
[Route("FiltroSiltDet")]
public HttpResponseMessage FiltroSiltDet(string DT_INCLUSAO)
{
try
{
HttpSessionState Session = HttpContext.Current.Session;
Session["DT_INCLUSAO"] = "test";
Session["DT_INCLUSAO"] = DT_INCLUSAO;
List<AcompanhamentoSiltDetDTO> retorno = new List<AcompanhamentoSiltDetDTO>();
using (AcompanhamentoSiltBLL oBLL = new AcompanhamentoSiltBLL())
{
retorno = oBLL.AcompanhamentoSiltTransacaoDet(DT_INCLUSAO);
}
var resp = Request.CreateResponse<List<AcompanhamentoSiltDetDTO>>(HttpStatusCode.OK, retorno);
return resp;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
[HttpPost]
[路线(“FiltroSiltDet”)]
公共HttpResponseMessageFiltRosiltDet(字符串DT_INCLUSAO)
{
尝试
{
HttpSessionState会话=HttpContext.Current.Session;
会话[“DT_INCLUSAO”]=“测试”;
会话[“DT_INCLUSAO”]=DT_INCLUSAO;
列表编号=新列表();
使用(acompanhanmentosiltlobll=new acompanhanmentosiltl())
{
Returno=公司的整体交通流量(DT_包括在内);
}
var resp=Request.CreateResponse(HttpStatusCode.OK,no);
返回响应;
}
捕获(例外情况除外)
{
抛出新异常(例如消息);
}
}
我总是这样使用会话,现在它不起作用了。当它试图将值传递给会话时,我得到了这个错误 对象引用未设置为对象的实例 有人知道它为什么不起作用
[HttpPost]
[Route("FiltroSiltDet")]
public HttpResponseMessage FiltroSiltDet(string DT_INCLUSAO)
{
try
{
HttpSessionState Session = HttpContext.Current.Session;
Session["DT_INCLUSAO"] = "test";
Session["DT_INCLUSAO"] = DT_INCLUSAO;
List<AcompanhamentoSiltDetDTO> retorno = new List<AcompanhamentoSiltDetDTO>();
using (AcompanhamentoSiltBLL oBLL = new AcompanhamentoSiltBLL())
{
retorno = oBLL.AcompanhamentoSiltTransacaoDet(DT_INCLUSAO);
}
var resp = Request.CreateResponse<List<AcompanhamentoSiltDetDTO>>(HttpStatusCode.OK, retorno);
return resp;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
我正在使用MVC应用程序,我总是在web窗体中使用此会话方法。我是Asp.Net MVC的新手。我通过向控制器类添加SessionState属性在MVC应用程序控制器中使用会话,请尝试此 向类添加[SessionState(SessionStateBehavior.Required)]属性 现在应该可以了 会话[“DT_INCLUSAO”]=“测试”
会话[“DT_INCLUSAO”]=DT_INCLUSAO 显示发生错误的确切消息和行。另外,您确定要抛出这样的新异常吗?在“session[“DT_INCLUSAO”]=”测试中,当它尝试将值传递给会话时,会出现错误“Object reference not set to a instance of a Object”“您确定要启用会话吗?在
Startup
中调用AddSession
和UseSession
?是的,仍然不工作,控制器在API中,我几乎可以肯定这就是为什么不工作的原因。遵循此操作,我希望它能帮助您。