我有一个用普通ASP.NET编写的应用程序,我想移植到ASP.NET MVC。
然而,我对持久化对象的正确位置感到困惑。我需要坚持几个理由:
通常,我会说#1将作为静态项保存在Globals.asax中,可以使用Global.Repository或类似的方式命中。
我通常会说#2应该是一个属性,在页面的基类中某个地方有一个会话备份存储。
现在我感到困惑的原因是,我听说MVC中的会话已经发生了变化,而且Global.asax不再拥有相同的类。此外,页面的概念已经被删除,因此向控制器的基类添加一个属性似乎.不对。
你们说什么?
发布于 2009-05-27 13:13:32
如果您使用会话,我建议您有一个session类,这样您只需要在代码中指定一次字符串名称,这也会给您IntelliSence。
public static class SessionHandler
{
// User Values
private static string _userID = "UserID";
private static string _userRole = "UserRole";
public static string UserID
{
get
{
if (HttpContext.Current.Session[SessionHandler._userID] == null)
{ return string.Empty; }
else
{ return HttpContext.Current.Session[SessionHandler._userID].ToString(); }
}
set
{ HttpContext.Current.Session[SessionHandler._userID] = value; }
}
public static string UserRole
{
get
{
if (HttpContext.Current.Session[SessionHandler._userRole] == null)
{ return string.Empty; }
else
{ return HttpContext.Current.Session[SessionHandler._userRole].ToString(); }
}
set
{ HttpContext.Current.Session[SessionHandler._userRole] = value; }
}
}https://stackoverflow.com/questions/915470
复制相似问题