Vis*_*til 8 .net c# asp.net-mvc session controller
问题陈述:
我正在尝试为MVC控制器中的会话对象分配一个值,因为对象引用没有设置为对象的实例,所以它给出异常.
我有两个控制器
当我在主控制器中为会话分配值时它工作正常.但是如果我在辅助控制器的Test()方法中分配相同的值,则它给出错误.
我在这里做错了什么???
主控制器:
public class MainController: Controller
{
SecondaryController secCont=new SecondaryController();
public ActionResult Index(LoginModel loginModel)
{
if (ModelState.IsValid)
{
Session["LogID"] = 10;
//This is working fine.
//Instead of this i want call secCont.Test(); method where value for session assigned, it is giving error.
}
return View(loginModel);
}
}
Run Code Online (Sandbox Code Playgroud)
二级控制器:
public class SecondaryController: Controller
{
public void Test()
{
Session["LogID"] = 10;
// Giving Error as **Object reference not set to an instance of an object.**
}
}
Run Code Online (Sandbox Code Playgroud)
小智 13
这是因为Session变量仅在Standard ASP.NET MVCController(主控制器)中可用.要访问辅助控制器中的会话变量,您应该使用
System.Web.HttpContext.Current.Session["LogID"] = 10;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12165 次 |
| 最近记录: |