如何正确重定向(在设置一个cookie)在MVC3 /剃刀? [英] How to properly redirect (while setting a cookie) in MVC3/Razor?

查看:344
本文介绍了如何正确重定向(在设置一个cookie)在MVC3 /剃刀?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我得到的感觉是Response.Redirect的是刚刚从传统的ASP遗留下来的,我应该用别的东西在MVC范例。

和第二,而我目前执行的Response.Redirect的工作时,它不设置我想要的饼干。我假设这是因为头被消灭了,而不是发送到客户端上的重定向。

下面是我到目前为止有:

  [HttpPost]
    公众的ActionResult登录(的FormCollection形式)
    {
        用户用户;
        串sessionKey;        如果(UserManager.Login(表格[电子邮件]的形式[密码]))
        {
            //登录的东西在这里            //记住用户的电子邮件
            。Response.Cookies [电子邮件]值=形式[电子邮件];
            。Response.Cookies [电子邮件] =到期DateTime.Now.AddDays(31);            //重定向到主页
            的Response.Redirect(〜/);
        }
     }


解决方案

在MVC重定向的正确方法是返回RedirectToAction(家,索引)

的饼干应该工作。

First off, I get the feeling that Response.Redirect is just a leftover from classic ASP, and I should be using something else in the MVC paradigm.

And second, while my current implementation of Response.Redirect IS working, it doesn't set the cookie I want it to. I'm assuming this is because the header gets wiped out instead of sent to the client on redirect.

Here is what I have so far:

    [HttpPost]
    public ActionResult Login(FormCollection form)
    {
        User user;
        string sessionKey;

        if (UserManager.Login(form["Email"], form["Password"]))
        {
            // Login stuff here

            // Remember user's email
            Response.Cookies["Email"].Value = form["Email"];
            Response.Cookies["Email"].Expires = DateTime.Now.AddDays(31);

            // Redirect to homepage
            Response.Redirect("~/");
        }
     }

解决方案

The proper way to redirect in MVC is return RedirectToAction("Home", "Index").

The cookies should work.

这篇关于如何正确重定向(在设置一个cookie)在MVC3 /剃刀?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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