Search code examples
aspnetboilerplate

how retrieve AbpSession in a MVC view


someone know how to retrieve the ApbSession in a mvc view??

i've tried with a session, or accessing the controllers that have the session injected, but there's no way to get the currentSession to get the userId and other properties.

The Asp.net Session is not valid, because ApbSession dont work with it.

Thanks in advice.


Solution

  • I suggest you to get needed values in the controller then pass to the view via viewmodel. However, if you need to access to AbpSession you have different ways depending on the ASP.NET version you are using.

    For ASP.NET Core, use @inject IAbpSession AbpSession in the razor view and directly use it.

    For ASP.NET MVC 5.x, use property injection in your view base (YourProjectNameWebViewPageBase like that: https://github.com/aspnetboilerplate/module-zero-template/blob/master/src/AbpCompanyName.AbpProjectName.WebMpa/Views/AbpProjectNameWebViewPageBase.cs). Alternatively, you can use IocManager.Instance.Resolve<IAbpSession>()... wherever you need.