Trying to pass Controller values to Global.asax.Cs to fill sessions

Asked

Viewed 186 times

3

When I try to pass some value from my controller to the Global.asax the values received in the aggregate are always zero.

Controller:

public class LoginController : Controller 
{
    DataContext db = new DataContext(); 
    public ActionResult Index()  
    {      
        MvcApplication M = new MvcApplication()
        var U = db.usuarios.single(u=> u.Id == 1);
        M.SetCarregarDadosUsuario(U);
    }
}

Global:

public class MvcApplication : System.Web.HttpApplication {
    public Usuarios usuarioSession = new Usuarios();

    protected void Application_AcquireRequestState(){
        if (Context.Handler is IRequiresSessionState || Context.Handler is IReadOnlySessionState){
            CarregarDadosUsuario();}  
        }
        private void CarregarDadosUsuario(){
            if(usuarioSession.Id >0){                    
                var loadU = GetCarregarDadosUsuario();                    
                HttpContext.Current.Session.Add("UsuarioImagem", loadU.UrlImg);                    
                HttpContext.Current.Session.Add("UsuarioNome", loadU.Nome);                    
                HttpContext.Current.Session.Add("UsuarioSessao", loadU.SessaoID);                    
                HttpContext.Current.Session.Add("UsuarioFilial", loadU.FilialID);            
            }        
        }        
        public void SetCarregarDadosUsuario(Usuarios user) {            
            this.usuarioSession = user;                           
        }        
        public Usuarios GetCarregarDadosUsuario(){            
        return this.usuarioSession;        
    }    
}

I would like to know how to pass a lavor from my Controller to Globlal.asax.Cs to fill my sessions.

Because the purpose of the sessions will be to add values in the layout file and set permissions not to perform query the database at all times. Example:

<div class="user-img-div user-basic basic-perfil-borda">
    <img src="/Content/NewTheme/img/@Session["UsuarioImagem"]" class="img-thumbnail" />
</div>
  • Why do you want to do it? Possibly not the best way to do it.

  • I want the sessions to use them later, in my _layout file, could tell me what would be the best way ?

1 answer

2


There are several ways to do this, I’ll give you two. You can do it with actual session, but you’re creating the session wrong.

Session

DataContext db = new DataContext(); 
public ActionResult Index()  
{      
    var U = db.usuarios.single(u=> u.Id == 1);
    Session["imagem"] = U.UrlImg;
    Session["nome"] = U.Nome;
    return View();
}

The view is correct:

<div class="user-img-div user-basic basic-perfil-borda">
    <img src="/Content/NewTheme/img/@Session["imagem"]" class="img-thumbnail" />
</div>

Cached

You can play in the cache the part that displays the user’s data, so the request with the database and processing of the View will be made only once for the set time. This will give more performance to your application.

  • Set a Partial View to display user data
  • Indicate that View is ChildActionOnly and define a OutputCache

In the file Usuariocontroller.Cs:

    // multiplicando segundos por minutos
    [OutputCache(Duration = 60 * 60, Location = OutputCacheLocation.ServerAndClient)]
    [ChildActionOnly]
    public ActionResult DadosUsuario()
    {
        var user = db.usuarios.single(u=> u.Id == 1);
        return View(user);
    }

View (Views/User/Dadosusuario.cshtml):

@model MeuProj.Usuario

<div class="user-img-div user-basic basic-perfil-borda">
        <img src="/Content/NewTheme/img/@model.UrlImg" class="img-thumbnail" />
</div>

To call Partial View, probably in _Layout.cshtml

@Html.Action("DadosUsuario", "UsuarioController")
  • Vlw bro, thanks, you helped a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.