title: ASP.NET MVC 3 - Detect Whether Current Page Was Redirected To in a Post Redirect Get Workflow
body: <p>In my C# .NET 4 MVC 3 application I have a delete controller for a set of CRUD pages which uses the <em>Post Redirect Get</em> pattern to redirect to an Index controller after a successful delete. I would like to render a button on the Index page only if this page was NOT redirected to by such an action. Is there a simple way to detect if the current page was redirected to (i.e. was reached as the result of a PRG redirect)?</p>
  
  <p>After reading <a href="http://blog.simonlovely.com/archive/2008/11/26/post-redirect-get-pattern-in-mvc.aspx" rel="nofollow">http://blog.simonlovely.com/archive/2008/11/26/post-redirect-get-pattern-in-mvc.aspx</a> my current approach is to set this in my delete controller with <code>TempData</code> after the <code>DeleteMyEntity</code> method has succeeded: </p>
  
  <pre><code>try {
      MyService.DeleteMyEntity(MyViewModel.MyEntity);
      TempData["Redirected"] = true;
      args = new RouteValueDictionary(new { Foo = 1, Baa = 2 });
      return RedirectToAction("Index", args);
  } catch (Exception e)
  {
     //Logging etc. - redirect should never be reached on exception (and TempData item not set)
     throw(e);
  }
  </code></pre>
  
  <p>then in my Index controller I check to see if this value exists and is true:</p>
  
  <pre><code>if (TempData["Redirected"] != null)
  {
      //we can then do something useful with this
  }
  </code></pre>
  
  <p>Another opportunity I see would be to add another item to <code>args</code> and check for this in the controller, but in this case I may as well just use <code>TempData</code>. Is there a way to do this using a HTTP Response code on the request without needing to pass this data through with <code>TempData</code> or a similar mechanism?</p>