我正在尝试在 MVC 5 中使用 Session 来传递最近注册用户的 Id
I'm trying to use Session in MVC 5 to pass the Id of a recently registered user
我正在尝试使用 MVC 5 中的会话来获取最近注册用户的 ID。
我有下面的代码,但它给我一个错误,说 "The name "Session" 在当前上下文中不存在"
我使用 HttpContext.Current.Session 添加了这条语句;然而它说 "The type or namespace could not be found" 我该怎么做才能解决这些错误?
public class RegisterBusiness
{
public UserManager<GlenwoodMed.Data.ApplicationUser> UserManager { get; set; }
public RegisterBusiness()
{
UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new IdentityDataContext()));
}
public bool FindUser(string userName, IAuthenticationManager authenticationManager)
{
var user = UserManager.FindByName(userName);
if (user != null)
{
return true;
}
return false;
}
public async Task<bool> RegisterUser(RegisterModel objRegisterModel, IAuthenticationManager authenticationManager)
{
var newuser = new ApplicationUser()
{
Id = objRegisterModel.UserName,
UserName = objRegisterModel.UserName,
Email = objRegisterModel.Email,
Password = objRegisterModel.Password,
FullName = objRegisterModel.FullName,
Surname = objRegisterModel.Surname,
MaritalStatus = objRegisterModel.MaritalStatus,
DOB = objRegisterModel.DOB,
Address = objRegisterModel.Address,
PostalCode = objRegisterModel.PostalCode,
Telephone = objRegisterModel.Telephone,
Employer = objRegisterModel.Employer,
EmployerTelephone = objRegisterModel.EmployerTelephone,
Occupation = objRegisterModel.Occupation,
NationalId = objRegisterModel.NationalId,
Status = objRegisterModel.Status,
MedicalAidName = objRegisterModel.MedicalAidName,
MedicalAidNo = objRegisterModel.MedicalAidNo
};
var result = await UserManager.CreateAsync(
newuser, objRegisterModel.Password);
if (result.Succeeded)
{
//Session["id"] = newuser.Id;
await SignInAsync(newuser, false, authenticationManager);
return true;
}
return false;
}
添加到它我有一个 class 来处理名为 RegisterBusiness 的注册操作,它位于我名为 BusinessLogic 的解决方案下的 class 库中,因此 Session 使用 System.Web.mvc 命名空间和我创建的 class 库中不存在的 .dll 我要添加到 BusinessLogic class 库的必要 .dll 是什么,因为我尝试只添加 System.Web.Mvc 但我认为它依赖于其他人,因为它还没有回应。你能进一步帮助吗?谢谢
根据 MSDN 站点,如果您使用的是 HttpContext.Current.Session,请尝试以下代码:
HttpContext context = HttpContext.Current;
context.Session["id"] = newuser.Id;
var id = (string)(context.Session["id"]);
您可以存储除字符串以外的其他类型,只需将会话转换为该类型即可。
根据您的代码:
Session["id"] = newuser.Id;
您可以在 Web 表单页面中使用这个 class。
编辑:
HttpContext.Current.Session 在 System.Web.Mvc 中。您为我们呈现的这个class可能是在网页之外的。我认为,您不能在 WebPage 类之外设置会话。这可以在 fe 中完成。控制器使用更简单的方式:Session["id"] = "id"。
所以我的想法是 returning True,return newuser.Id
并将其分配给 Class 中的会话,从那里调用此注册函数。
编辑2:
上面的解决方案是当标准
System.Web.HttpContext.Current.Session["Id"] = newuser.Id;
无效。首先尝试这个,但不要添加任何 Using 语句。你只需要 System.Web.Mvc;到 运行 这个。
您已尝试通过让单独的层(项目)在此处执行工作来分离关注点。这很好 - 让我们更进一步。为什么这一层应该知道您正在使用 Web 设置?
如果你真的想把关注点分开,你可以从方法中 return int?
然后把结果(如果不为空)放在 Session
一旦你进入又是 Web 层。
如果在此级别上确实不需要分离,您可以将对 System.Web
的引用添加到该项目。但是在这样做之前一定要三思这个 class 库的职责。
我建议使用 IoC (Inversion of Control)(由任何依赖注入框架实现,如 Unity 或 Autofac 等),而不是试图让您的对象智能地了解它所需的具体类型:
public class RegisterBusiness
{
// This most likely shouldn't be public or a property
private UserManager<ApplicationUser> _userManager;
private HttpSessionStateBase _session;
public RegisterBusiness(UserManager<ApplicationUser> userManager,
HttpSessionStateBase session)
{
_userManager = userManager;
_session = session;
}
public async Task<bool> RegisterUser(RegisterModel objRegisterModel,
IAuthenticationManager authenticationManager)
{
var newuser = new ApplicationUser()
{
//...
if (result.Succeeded)
{
_session["id"] = newuser.Id;
然后在您的 RegisterBusiness 的构造函数中:
public MyController : AsyncController
{
public Task<ActionResult> MyAction(...)
{
// if you don't use DI
var userManager = new UserManager<ApplicationUser>(
new UserStore<ApplicationUser>(
new IdentityDataContext()));
var registerBusiness = new RegisterBusiness(userManager, Session);
var result = await registerBusiness.RegisterUser(...);
}
}
我还建议永远不要使用 HttpContext.Current
它太过时了,不容易进行单元测试,这就是为什么较新的 MVC 框架实现了 HttpSessionStateBase
,它是 Session 的包装器。
我正在尝试使用 MVC 5 中的会话来获取最近注册用户的 ID。 我有下面的代码,但它给我一个错误,说 "The name "Session" 在当前上下文中不存在"
我使用 HttpContext.Current.Session 添加了这条语句;然而它说 "The type or namespace could not be found" 我该怎么做才能解决这些错误?
public class RegisterBusiness
{
public UserManager<GlenwoodMed.Data.ApplicationUser> UserManager { get; set; }
public RegisterBusiness()
{
UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new IdentityDataContext()));
}
public bool FindUser(string userName, IAuthenticationManager authenticationManager)
{
var user = UserManager.FindByName(userName);
if (user != null)
{
return true;
}
return false;
}
public async Task<bool> RegisterUser(RegisterModel objRegisterModel, IAuthenticationManager authenticationManager)
{
var newuser = new ApplicationUser()
{
Id = objRegisterModel.UserName,
UserName = objRegisterModel.UserName,
Email = objRegisterModel.Email,
Password = objRegisterModel.Password,
FullName = objRegisterModel.FullName,
Surname = objRegisterModel.Surname,
MaritalStatus = objRegisterModel.MaritalStatus,
DOB = objRegisterModel.DOB,
Address = objRegisterModel.Address,
PostalCode = objRegisterModel.PostalCode,
Telephone = objRegisterModel.Telephone,
Employer = objRegisterModel.Employer,
EmployerTelephone = objRegisterModel.EmployerTelephone,
Occupation = objRegisterModel.Occupation,
NationalId = objRegisterModel.NationalId,
Status = objRegisterModel.Status,
MedicalAidName = objRegisterModel.MedicalAidName,
MedicalAidNo = objRegisterModel.MedicalAidNo
};
var result = await UserManager.CreateAsync(
newuser, objRegisterModel.Password);
if (result.Succeeded)
{
//Session["id"] = newuser.Id;
await SignInAsync(newuser, false, authenticationManager);
return true;
}
return false;
}
添加到它我有一个 class 来处理名为 RegisterBusiness 的注册操作,它位于我名为 BusinessLogic 的解决方案下的 class 库中,因此 Session 使用 System.Web.mvc 命名空间和我创建的 class 库中不存在的 .dll 我要添加到 BusinessLogic class 库的必要 .dll 是什么,因为我尝试只添加 System.Web.Mvc 但我认为它依赖于其他人,因为它还没有回应。你能进一步帮助吗?谢谢
根据 MSDN 站点,如果您使用的是 HttpContext.Current.Session,请尝试以下代码:
HttpContext context = HttpContext.Current;
context.Session["id"] = newuser.Id;
var id = (string)(context.Session["id"]);
您可以存储除字符串以外的其他类型,只需将会话转换为该类型即可。
根据您的代码:
Session["id"] = newuser.Id;
您可以在 Web 表单页面中使用这个 class。
编辑:
HttpContext.Current.Session 在 System.Web.Mvc 中。您为我们呈现的这个class可能是在网页之外的。我认为,您不能在 WebPage 类之外设置会话。这可以在 fe 中完成。控制器使用更简单的方式:Session["id"] = "id"。
所以我的想法是 returning True,return newuser.Id
并将其分配给 Class 中的会话,从那里调用此注册函数。
编辑2: 上面的解决方案是当标准
System.Web.HttpContext.Current.Session["Id"] = newuser.Id;
无效。首先尝试这个,但不要添加任何 Using 语句。你只需要 System.Web.Mvc;到 运行 这个。
您已尝试通过让单独的层(项目)在此处执行工作来分离关注点。这很好 - 让我们更进一步。为什么这一层应该知道您正在使用 Web 设置?
如果你真的想把关注点分开,你可以从方法中 return int?
然后把结果(如果不为空)放在 Session
一旦你进入又是 Web 层。
如果在此级别上确实不需要分离,您可以将对 System.Web
的引用添加到该项目。但是在这样做之前一定要三思这个 class 库的职责。
我建议使用 IoC (Inversion of Control)(由任何依赖注入框架实现,如 Unity 或 Autofac 等),而不是试图让您的对象智能地了解它所需的具体类型:
public class RegisterBusiness
{
// This most likely shouldn't be public or a property
private UserManager<ApplicationUser> _userManager;
private HttpSessionStateBase _session;
public RegisterBusiness(UserManager<ApplicationUser> userManager,
HttpSessionStateBase session)
{
_userManager = userManager;
_session = session;
}
public async Task<bool> RegisterUser(RegisterModel objRegisterModel,
IAuthenticationManager authenticationManager)
{
var newuser = new ApplicationUser()
{
//...
if (result.Succeeded)
{
_session["id"] = newuser.Id;
然后在您的 RegisterBusiness 的构造函数中:
public MyController : AsyncController
{
public Task<ActionResult> MyAction(...)
{
// if you don't use DI
var userManager = new UserManager<ApplicationUser>(
new UserStore<ApplicationUser>(
new IdentityDataContext()));
var registerBusiness = new RegisterBusiness(userManager, Session);
var result = await registerBusiness.RegisterUser(...);
}
}
我还建议永远不要使用 HttpContext.Current
它太过时了,不容易进行单元测试,这就是为什么较新的 MVC 框架实现了 HttpSessionStateBase
,它是 Session 的包装器。