Attempting to pass Controller values to Global.asax.cs to fill in the sessions

3

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

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 I can move my Controller to Globlal.asax.cs to fill out my sessions.

Because the purpose of sessions is to add values in the layout file and set permissions to not perform benchmarking 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>
    
asked by anonymous 09.12.2016 / 13:46

1 answer

2

There are several ways to do this, I'll give you two. You can do it with the same session, however you are creating the session in the wrong way.

With 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>

With Cache

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

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

In the UserController.cs file:

    // 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 / UserData.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 the Partial View, probably in _Layout.cshtml

@Html.Action("DadosUsuario", "UsuarioController")
    
09.12.2016 / 16:44