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.
– Jéf Bueno
I want the sessions to use them later, in my _layout file, could tell me what would be the best way ?
– Samuel Aalmeida