c# – Trying to pass values ​​from Controller to Global.asax.cs to populate sessions

Question:

When I try to pass some value from my controller to Global.asax the values ​​received in 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 pass a labor from my Controller to Globlal.asax.cs to fill my sessions.

Because the purpose of the sessions will be to add values ​​to the layout file and set permissions so as not to 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>

Answer:

There are several ways to do this, I'll give you two. You can do it with a session anyway, but 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 cache the part that displays the user's data, so the request with the database and View processing will be done only once for the defined time. This will give your application more performance.

  • Define a Partial View to display user data
  • Indicate that this View is ChildActionOnly and define an 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/Usuario/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 the Partial View, probably in _Layout.cshtml

@Html.Action("DadosUsuario", "UsuarioController")
Scroll to Top