0

I'm trying to use Session but the normally way i do its not working.

[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);
    }
}

I always used session this way and its not working now.
When it try to pass value to the session I get this error.

Object reference not set to an instance of an object

Someone know what why its not working.

I'm using a MVC application, I Always used this session method in web Forms I'm new in Asp.Net MVC.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • 1
    Show the exact error message and line where it happens. Also are you sure you want to throw a new exception like that? – Sami Kuhmonen Mar 31 '17 at 17:35
  • I get the error "Object reference not set to an instance of an object" when it try to pass value to the session in "Session["DT_INCLUSAO"] = "test" – Vinicius Cano Mar 31 '17 at 17:49
  • Are you sure that you enable session? Calls like `AddSession` and `UseSession` in `Startup`? – Dmytro Bogatov Mar 31 '17 at 19:51
  • Yes,still not working, the controllers is inside a API, i'm almost sure that's why doesn't work. – Vinicius Cano Mar 31 '17 at 19:59
  • Follow this http://stackoverflow.com/a/11479021/1365663, I hope it will help you. – Jignesh Variya Apr 03 '17 at 06:49
  • @ViniciusCano what does `does not work mean`? In what scenario are you calling the action? how was the service configured? Provide a [mcve] that can be used to reproduce the problem. Not much help can be provided otherwise. – Nkosi Apr 04 '17 at 03:07

1 Answers1

0

I used Session in my MVC application controller by adding SessionState attribute to controller class, Please try this...

Add [SessionState(SessionStateBehavior.Required)] attribute to your class

now this should work

Session["DT_INCLUSAO"] = "test";

Session["DT_INCLUSAO"] = DT_INCLUSAO;

Rex Andrew
  • 388
  • 2
  • 8