Using Session in an MVC controller returns error

2

I'm trying to use a session in an MVC application, I believe you're right but you're returning the error:

  

Object reference not set to an instance of an object

When it tries to pass the value in the session and the value is not null.

I think this is not the proper way to use MVC session, does anyone know what I'm doing wrong?

[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);
            }
        }
    
asked by anonymous 31.03.2017 / 19:07

1 answer

1

Can not access context by HttpContext.Current in ApiController .

To access the context use the following form:

var context = Request.Properties["MS_HttpContext"] as HttpContext;

Font

Note: The property name may have changed, check other properties in Request.Properties

    
01.04.2017 / 01:11