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.
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...
Is this a common practice? Is there no better solution? Should I handle my cookies differently?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With