我有一个会话辅助类,
namespace ShopCart.BAL
{
public static class SessionHelper
{
public static ShopUser myShopUser
{
get
{
return HttpContext.Current.Session["myShopUser"] as ShopUser;
}
set
{
HttpContext.Current.Session["myShopUser"] = value;
}
}
}
}
另外ShopUser类看起来像
public class ShopUser
{
public string LoginID { get; set; }
public string SessionID { get; set; }
public long UserId { get; set; }
public long ShoppingCartId { get; set; }
public bool? Status { get; set; }
}
现在我想知道如何为特定属性设置会话? SessionHelper.Status = false; //它在SessionHelper中创建一个新属性 如何使用上面的类来创建会话?
@mohsen你的回答是正确的。 此外,
public ActionResult Index() {
ShopLogin objShopLogin = new ShopLogin ();
objShopLogin .checkUser();
}
public class ShopLogin {
public string checkUser()
{
SessionHelper.myShopUser.Status = false;
}
}
正如您所看到的,我在这里创建类对象,如ShopLogin objShopLogin = new ShopLogin(); 你能告诉我,我可以在这里使用依赖注入来避免创建像这样的类对象吗?
答案 0 :(得分:2)
使用
SessionHelper.myShopUser.Status = false
修改强>
是的。只需将static
修饰符添加到CheckUser
方法
public ActionResult Index() {
ShopLogin.checkUser();
}
public class ShopLogin {
public static string checkUser()
{
SessionHelper.myShopUser.Status = false;
}
}