GARANTIR DESCONTO

Fórum USO DE HttpContext.Current.Session numa Classe #27247

12/03/2010

0

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 "";
 }
}
Sidney Mendonça/

Sidney Mendonça/

Responder

Posts

12/03/2010

Fabio Mans

Olá Sidney

Faça referência a Namespace abaixo.

using System.Web;

Espero ter ajudado.

Fabio
Responder

Gostei + 0

12/03/2010

Fabio Mans

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;
            }
Responder

Gostei + 0

12/03/2010

Fabio Mans

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();

    }
Responder

Gostei + 0

15/03/2010

Fabio Mans

Conseguiu resolver?

Responder

Gostei + 0

15/03/2010

Sidney Mendonça/

OK.   Obrigado.
Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar