调用RedirectToAction时,Response.Cookies会重置 [英] Response.Cookies gets reset when RedirectToAction is called

查看:69
本文介绍了调用RedirectToAction时,Response.Cookies会重置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的asp.net-mvc项目中,我有一个AccountController,它在登录时将带有用户首选项的cookie设置为Request.Response,然后执行RedirectToAction. 重定向后,Cookie会重置,因此我会丢失设置.

IN my asp.net-mvc project I have an AccountController that upon logging in sets a cookie with user preferences to the Request.Response and then does a RedirectToAction. Upon redirecting, the cookies are reset so I loose my settings.

我能想到的唯一解决方案是将来自cookie的数据添加到tempdata中,然后稍后在RedirectToAction的目标操作中再次获取它.这当然是有点倒退了……

The only solution I can come up with is adding the data from the cookie in the tempdata and then fetching it again later in the RedirectToAction's target action. This is off course a little backwards...

这是常见的做法吗?有没有更好的解决方案?我应该以不同的方式处理我的Cookie吗?

Is this a common practice? Is there no better solution? Should I handle my cookies differently?

推荐答案

,为此使用TempData是很常见的做法,这与TempData的假设非常吻合使用-在仅由客户端重定向分隔的两个操作方法之间传递临时数据.

Yes, using TempData for this is a common practice, and this is quite in line with how TempData is supposed to be used - passing temporary data between two action methods separated only by a client redirect.

由于可以从登录页面重定向到任何其他操作方法,因此您可以在基本控制器中实现从TempData设置cookie的功能.这将使任何操作方法都符合cookies-via-TempData.这是在页面上显示通知时的一种常见情况,其中通知(例如此网站上的通知)通常会自动从TempDataViewData 视图.

Since the redirect from the login page could be to any other action method, you could implement the functionality to set cookies from TempData in your base controller. This would make any action method cookies-via-TempData compliant. This is a common scenario when displaying notifications on pages, where the notification (like the notifications on this site) would usually travel from TempData to ViewData to the view automatically.

这篇关于调用RedirectToAction时,Response.Cookies会重置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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