当人们在我的ASP.NET MVC网站上注册时,我通过电子邮件发送激活链接,以便他们可以激活他们的帐户并登录。我想在注册成功并发送电子邮件时显示一条消息。为此,我重定向到另一页。
当人们直接访问此网址时,我不想显示此页面,因为它不是普通网页。我使用TempData来检查它们是否来自注册页面。
public ActionResult Register()
{
AccountRegisterView accountView = InitializeAccountRegisterViewWithIssue(false, "");
return View(accountView);
}
[HttpPost]
public ActionResult Register(AccountRegisterView accountView)
{
if (!ModelState.IsValid)
{
return View(accountView);
}
// Register user and send activation link via email...
TempData["success"] = true;
return RedirectToAction("RegisterEmail");
}
public ActionResult RegisterEmail()
{
if (TempData["success"] != null)
{
return View();
}
return RedirectToAction("Login");
}
我想知道这是否是最佳做法。或者我应该采用不同的方式吗?
答案 0 :(得分:1)
我认为这是对TempData[]
的完美使用。您需要一个变量用于一次性请求,以决定是否应该显示该页面。
我想你可以使用会话变量,但是你需要记住清除会话。您可以使用某种类型的注册密钥,但是您也需要跟踪这些密钥。长话短说,不好,你很好。