我创建了一个基本Page
类并尝试将对象保存在 session 变量中,但它给了我对象引用未设置为对象的实例。
错误。我的对象已初始化并具有值。
我在 Web.config 文件中添加了 enableSessionState="true"
和 sessionState mode="InProc"
设置。
ASP.NET State Service 也在运行
using System;
using System.Web;
using System.Globalization;
using System.Threading;
using System.Xml.Linq;
public class BasePage : System.Web.UI.Page
{
public BasePage()
{
string catalog = "" + HttpContext.Current.Request.QueryString["c"];
if (catalog != "")
{
Engine eng = new Engine();
TEngine engine = eng.GetEngines(catalog.ToUpper());
if (engine != null)
{
HttpContext.Current.Session["CurrentEngine"] = engine;
}
}
}
}
网络配置:
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<sessionState mode="InProc" timeout="60" cookieless="false" />
<pages enableSessionState="true">
<controls>
<add src="~/Components/menu.ascx" tagName="Menu" tagPrefix="uc" />
<add src="~/Components/footer.ascx" tagName="Footer" tagPrefix="uc" />
<add src="~/Component/tree.ascx" tagName="Tree" tagPrefix="uc" />
</controls>
</pages>
<httpModules>
<add type="System.Web.SessionState.SessionStateModule" name="Session"/>
</httpModules>
</system.web>
</configuration>
最佳答案
根据this MSDN document ,Page
的属性(Context
、Request
等)将在 Page 对象构造后分配。您可以将使用这些属性的代码移至 Init
或 Load
事件处理程序。
并且无需在网页内使用HttpContext.Current
。只需使用 this.Context
。
关于asp.net - 无法在 Asp.net 中保存 session ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25000700/