我可以为MVC中的所有操作结果创建一个会话 [英] Can I have one session for all action result in MVC
本文介绍了我可以为MVC中的所有操作结果创建一个会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在研究MVC并创建了一个会话来检查登录。我创建了一个会话,并且在索引操作上已经应用了身份验证。
只是想要对索引的所有操作结果做同样的事情。我可以为单个会话检查创建一个代码,说Session [LoginUserName]并检查多个ActionResults的值。
我尝试过:
尝试使用单个actionresult创建会话检查
I Was working on MVC and created a session just to check for Login. I have created a session and on Index action have applied the authentication.
Just Wanted the do same thing with all the action results with Index. Can I create a single code for single session check say "Session[LoginUserName]" and check the value for multiple ActionResults.
What I have tried:
Tried Creating a Session with single actionresult check
推荐答案
You need to call them either in the constructor or in a method of your controller.
Furthermore those variables should have been set somewhere or their vaues will stay null.
According to your example you will need to set your Session["SessionClass"] key somewhere before calling it in the constructor:
public ActionResult Details()
{
Session["SessionClass"] = new MyClass() { // Set your class properties };
return View((MyClass)Session["SessionClass"]);
}
Now we will unbox that saved value from the session:
public HomeController()
{
MyClass test = (MyClass)Session["SessionClass"];
// Do stuff with `test` now
}
This should work fine within your controller.
这篇关于我可以为MVC中的所有操作结果创建一个会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文