在MVC中为什么我不能为控制器的会话添加值? [英] In MVC why I cannot add a value to a session of a controller?

查看:74
本文介绍了在MVC中为什么我不能为控制器的会话添加值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此代码中:

In this code:

CardexController controller = new CardexController();
controller.Session.Add("cmstr", id);
controller.Index();



我在行上遇到 System.NullReferenceException 错误:

controller.Session.Add("cmstr", id);





当id有价值而且它不是空的时候我该怎么办?



我是什么尝试过:



会话听起来像一个界面,所以我无法创建它的新实例。而且我也无法为它添加值。



What should I do while id has value and it is not empty?

What I have tried:

Session sounds like an interface so I cannot make a new instance of it. And also I cannot add a value to it.

推荐答案

会话你的财产 CardexController 实例是空的,毫无疑问。

现在是时候在初始化控制器的行上设置一个断点,然后按F5。



Session属性需要一个实现给定接口的对象的实例(我只是从你写的内容中推断,因为我没有类的正式定义),这并不意味着你不必为它提供有效的实例。
Session property of you CardexController instance is null, undoubtably.
It's time for you to put a breakpoint on the line which initializes the controller, and press F5.

The fact that the Session property expects some instance of an object which implements a given interface (I just extrapolate from what you wrote, as I do not have the formal definition of the class), does not mean that you do not have to provide it with a valid instance.


这篇关于在MVC中为什么我不能为控制器的会话添加值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆