您不应将用户存储在Session中。当通过修改web.config或达到内存限制重新启动应用程序时,会话可能很容易丢失。这将在随机时刻注销用户。
没有理由不使用会话用于不同的目的(例如将项目存储在购物篮中)。你可以那样做:
首先我们定义的接口:
public interface ISessionWrapper
{
int SomeInteger { get; set; }
}
然后我们做的HttpContext执行:
public class HttpContextSessionWrapper : ISessionWrapper
{
private T GetFromSession<T>(string key)
{
return (T) HttpContext.Current.Session[key];
}
private void SetInSession(string key, object value)
{
HttpContext.Current.Session[key] = value;
}
public int SomeInteger
{
get { return GetFromSession<int>("SomeInteger"); }
set { SetInSession("SomeInteger", value); }
}
}
然后我们定义我们的基本控制器:
public class BaseController : Controller
{
public ISessionWrapper SessionWrapper { get; set; }
public BaseController()
{
SessionWrapper = new HttpContextSessionWrapper();
}
}
最后:
public ActionResult SomeAction(int myNum)
{
SessionWrapper.SomeInteger
}
这将使测试变得简单,因为您可以用控制器测试中的模拟代替ISessionWrapper。
我读过你对另一个问题的回答,我还阅读了REST介绍文章(非常好,内容翔实!),我会赞同所有提出的观点。直到现在,我还没有考虑将用户数据视为违反RESTful原则。我不能保证我会为这个特定的应用程序采用这些原则(尽管我会将其作为一种可能性进行研究),但我一定会努力在将来更加严格地遵守RESTful原则。 – DaveDev 2010-02-06 14:09:51