成功发布后显示消息的RedirectToAction [英] RedirectToAction after successful post to show a message

查看:123
本文介绍了成功发布后显示消息的RedirectToAction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当人们在我的ASP.NET MVC网站上注册时,我会通过电子邮件发送激活链接,以便他们可以激活其帐户并登录.注册成功并发送电子邮件后,我想显示一条消息.为此,我重定向到另一个页面.

When people register on my ASP.NET MVC website I send an activation link via email so they can activate their account and login. I want to show a message when the registration is successful and the email is sent. To do that I redirect to another page.

当人们直接进入该URL时,我不想显示该页面,因为它不是正常页面.我使用TempData来检查它们是否来自注册页面.

I do not want to show this page when people go directly to this URL because it is not a normal page. I use TempData to check if they are coming from the registration page.

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");
}

我想知道这是否被认为是最佳实践.还是我应该以不同的方式来做?

I would like to know if this is considered best practice. Or should I do this differently?

推荐答案

我认为这是TempData[]的完美用法.您需要一次请求一个变量来决定是否显示该页面.

I think this is a perfectly good use of TempData[]. You need a variable for a one time request to decide if you should show the page or not.

我想您可以使用会话变量,但是您需要记住要清除会话.您可以使用某种类型的注册密钥,但随后还需要跟踪它们.长话短说,不,你很好.

I suppose you could use a session variable, but you would need to remember to clear the session. You could use some type of registration key, but then you would need to track those as well. Long story short, nope, you are good.

这篇关于成功发布后显示消息的RedirectToAction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆