Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RedirectToAction Bug?

I Have following code:

Controller:

public ActionResult Step1()
{
        return View();
}

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Step1(FormCollection form)
{
        TempData["messageStatus"] = new Random().Next(1, 1000);
        return RedirectToAction("Step1");
}

View:

<%=TempData["messageStatus"]%>

in first time in view im getting 12345 for example, but when i request second time of course i must get something else instead 12345 for example 54321 but not, im getting same result 12345, how can u explain it? RedirectToAction cache pages?

where does it mean i must put Guid in my urls for resolving problems with cache? what do u think about this issue?

like image 636
Sasha Avatar asked Jul 20 '26 05:07

Sasha


2 Answers

I'm guessing you're running into caching problems. It's not a problem with redirect to action. All RedirectToAction does is issues a redirect response to your browser telling it to request Step01. Then your browser makes a request for Step01.

In that case, your browser might have Step01 cached. So you need to add a Response header in STep01 indicating it should never be cached. You can do this like so:

[OutputCache(Location = OutputCacheLocation.None)]
public ActionResult Step1()
{
        return View();
}

Or you can add a random querystring to the redirect to action call by passing in an arbitrary value.

like image 195
Haacked Avatar answered Jul 21 '26 20:07

Haacked


Don't new up a new Random object every time. Use the same one. Remember, the .Net Random is only a Pseudo random number generator.

like image 23
BFree Avatar answered Jul 21 '26 19:07

BFree



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!