USO DE HttpContext.Current.Session numa Classe
Estou tentando utilizar o (HttpContext.Current.Session numa classe, e gera uma exception Object reference not set to an instance of an object.
Usando o Session numa pagina aspx funciona normalmente
A Session tem conteudo, já verifiquei fora da classe. O que está errado.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; /// <summary>
/// Summary description for Class1
/// </summary>
public class Class1
{
public string Retorno()
{
if (HttpContext.Current.Session["Content"] != null)
return (string)HttpContext.Current.Session["Content"];
else
return "";
}
}
Usando o Session numa pagina aspx funciona normalmente
A Session tem conteudo, já verifiquei fora da classe. O que está errado.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; /// <summary>
/// Summary description for Class1
/// </summary>
public class Class1
{
public string Retorno()
{
if (HttpContext.Current.Session["Content"] != null)
return (string)HttpContext.Current.Session["Content"];
else
return "";
}
}
Sidney Mendonça/
Curtidas 0
Respostas
Fabio Mans
12/03/2010
Olá Sidney
Faça referência a Namespace abaixo.
using System.Web;
Espero ter ajudado.
Fabio
Faça referência a Namespace abaixo.
using System.Web;
Espero ter ajudado.
Fabio
GOSTEI 0
Fabio Mans
12/03/2010
Vi que você já fez a referência, tente algo assim
public string Retorno()
{
if (HttpContext.Current.Session != null)
if (HttpContext.Current.Session["Content"] != null)
return (string)HttpContext.Current.Session["Content"];
else
return "";
return null;
}
public string Retorno()
{
if (HttpContext.Current.Session != null)
if (HttpContext.Current.Session["Content"] != null)
return (string)HttpContext.Current.Session["Content"];
else
return "";
return null;
}
GOSTEI 0
Fabio Mans
12/03/2010
Testei rapidamente na minha aplicação e retornou normalmente.
using System.Text;
using System.Web;
namespace GrupoSBF.Corporativo
{
public class EnvioEmail
{
public string Retorno()
{
if (HttpContext.Current.Session != null)
if (HttpContext.Current.Session["Content"] != null)
return (string)HttpContext.Current.Session["Content"];
else
return "";
return null;
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
Session["Content"] = "teste";
EnvioEmail obj = new EnvioEmail();
string teset = obj.Retorno();
}
using System.Text;
using System.Web;
namespace GrupoSBF.Corporativo
{
public class EnvioEmail
{
public string Retorno()
{
if (HttpContext.Current.Session != null)
if (HttpContext.Current.Session["Content"] != null)
return (string)HttpContext.Current.Session["Content"];
else
return "";
return null;
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
Session["Content"] = "teste";
EnvioEmail obj = new EnvioEmail();
string teset = obj.Retorno();
}
GOSTEI 0
Fabio Mans
12/03/2010
Conseguiu resolver?
GOSTEI 0
Sidney Mendonça/
12/03/2010
OK.
Obrigado.
GOSTEI 0