views:

567

answers:

3

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?

+1  A: 

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.

BFree
i used random for example only, i prefer guid :)
msony
A: 

Try this:

TempData["messageStatus"] = new Random(DateTime.Now.Millisecond).Next(1, 1000);
Todd Smith
There's no overload for Random that takes a DateTime. It takes an int, so you can use DateTime.Now.Millisecond.
BFree
but i dont want improve random method, i just want know that its ok that redirect cache pages
msony
+4  A: 

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.

Haacked
thanks i will use outputcache in my base controller instead random
msony