<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: ASP.NET MVC TempData Is Really RedirectData</title>
	<atom:link href="http://blogs.teamb.com/craigstuntz/2009/01/23/37947/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.teamb.com/craigstuntz/2009/01/23/37947/</link>
	<description>C# • Delphi • Entity Framework • Functional Programming • InterBase • MVC • .NET • Web</description>
	<pubDate>Thu, 18 Mar 2010 03:13:51 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
		<item>
		<title>By: Craig Stuntz</title>
		<link>http://blogs.teamb.com/craigstuntz/2009/01/23/37947/#comment-5186</link>
		<dc:creator>Craig Stuntz</dc:creator>
		<pubDate>Mon, 06 Apr 2009 10:28:57 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.teamb.com/craigstuntz/?p=37947#comment-5186</guid>
		<description>Josh, there's nothing wrong with using query string parameters, as with your first suggestion, except that, as you note, there is a maximum length, and the value is exposed to the end-user. Since the action itself may use query string parameters, it's hard to know how much you can add reliably. Worse, the maximum length is browser-specific.

As for TempData size and session data size, they are the same, because TempData &lt;i&gt;is&lt;/i&gt; session. I would not suggest using session in lieu of TempData, though, because, as I said, TempData already uses session, and using TempData makes your intentions clear.</description>
		<content:encoded><![CDATA[<p>Josh, there&#8217;s nothing wrong with using query string parameters, as with your first suggestion, except that, as you note, there is a maximum length, and the value is exposed to the end-user. Since the action itself may use query string parameters, it&#8217;s hard to know how much you can add reliably. Worse, the maximum length is browser-specific.</p>
<p>As for TempData size and session data size, they are the same, because TempData <i>is</i> session. I would not suggest using session in lieu of TempData, though, because, as I said, TempData already uses session, and using TempData makes your intentions clear.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josh Comley</title>
		<link>http://blogs.teamb.com/craigstuntz/2009/01/23/37947/#comment-5184</link>
		<dc:creator>Josh Comley</dc:creator>
		<pubDate>Mon, 06 Apr 2009 08:59:34 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.teamb.com/craigstuntz/?p=37947#comment-5184</guid>
		<description>You could use the routeValues:

RedirectToAction("MyAction", new MyValues() { ErrorType = 1, Test = true });

This will put the values in the URL. For small amounts of data, this solves the problem without a race condition.

If you're really, really concerned about the race condition and want to store more data, or don't want sensitive server-side only data passed via URL, you could do something like this:

Guid guid = Guid.NewGuid();
Session[guid.ToString()] = myData;
RedirectToAction("MyAction", new MyValues() { g=guid.ToString() });

And then in your view, check the session for a value named with the Guid passed as the "g" parameter in the URL.

As soon as the view starts rendering you could grab the session data, pull it into the view and clear out the session.

If it cannot find that guid then they have either visited the same guid URL twice, or their session has expired.

Like Craig points out, the latter shouldn't be a problem because if you only use this technique on redirects then your session is not going to timeout.

If the guid is not found, fall back to the default implementation of the view.

However, if you find yourself ever needing to use this situation then you're probably doing something more deep rooted wrong. Have a think about your whole process - this should be a last resort tool in an obscure situation. Most problems can be resolved in a much purer fashion!

I haven't considered side issues like session data maximum size, but then temp data maximum size was never discussed, and to be honest I don't know the details!

Awaiting Hole Picking :)</description>
		<content:encoded><![CDATA[<p>You could use the routeValues:</p>
<p>RedirectToAction("MyAction", new MyValues() { ErrorType = 1, Test = true });</p>
<p>This will put the values in the URL. For small amounts of data, this solves the problem without a race condition.</p>
<p>If you&#8217;re really, really concerned about the race condition and want to store more data, or don&#8217;t want sensitive server-side only data passed via URL, you could do something like this:</p>
<p>Guid guid = Guid.NewGuid();<br />
Session[guid.ToString()] = myData;<br />
RedirectToAction("MyAction", new MyValues() { g=guid.ToString() });</p>
<p>And then in your view, check the session for a value named with the Guid passed as the "g" parameter in the URL.</p>
<p>As soon as the view starts rendering you could grab the session data, pull it into the view and clear out the session.</p>
<p>If it cannot find that guid then they have either visited the same guid URL twice, or their session has expired.</p>
<p>Like Craig points out, the latter shouldn&#8217;t be a problem because if you only use this technique on redirects then your session is not going to timeout.</p>
<p>If the guid is not found, fall back to the default implementation of the view.</p>
<p>However, if you find yourself ever needing to use this situation then you&#8217;re probably doing something more deep rooted wrong. Have a think about your whole process - this should be a last resort tool in an obscure situation. Most problems can be resolved in a much purer fashion!</p>
<p>I haven&#8217;t considered side issues like session data maximum size, but then temp data maximum size was never discussed, and to be honest I don&#8217;t know the details!</p>
<p>Awaiting Hole Picking <img src='http://blogs.teamb.com/craigstuntz/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Craig Stuntz</title>
		<link>http://blogs.teamb.com/craigstuntz/2009/01/23/37947/#comment-4444</link>
		<dc:creator>Craig Stuntz</dc:creator>
		<pubDate>Mon, 02 Feb 2009 20:15:42 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.teamb.com/craigstuntz/?p=37947#comment-4444</guid>
		<description>One more thing: I think it's arguably a bug that TempData is cleared during an AJAX request. Given that the purpose of TempData is to provide an alternative to ViewData suitable for handling redirects, it's probably never correct to clear it out on an AJAX request, which will never be a response to a redirect. The framework has a Request.IsAjaxRequest property, which could conceivably be used to determine whether or not clear TempData at the end of the request.</description>
		<content:encoded><![CDATA[<p>One more thing: I think it&#8217;s arguably a bug that TempData is cleared during an AJAX request. Given that the purpose of TempData is to provide an alternative to ViewData suitable for handling redirects, it&#8217;s probably never correct to clear it out on an AJAX request, which will never be a response to a redirect. The framework has a Request.IsAjaxRequest property, which could conceivably be used to determine whether or not clear TempData at the end of the request.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Craig Stuntz</title>
		<link>http://blogs.teamb.com/craigstuntz/2009/01/23/37947/#comment-4443</link>
		<dc:creator>Craig Stuntz</dc:creator>
		<pubDate>Mon, 02 Feb 2009 19:39:24 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.teamb.com/craigstuntz/?p=37947#comment-4443</guid>
		<description>&lt;i&gt;Craig, I’d rather disagree that "close to impossible" is an acceptable way of dealing with reliability issues.&lt;/i&gt;

Wait a second, George; you're misquoting me.

Nowhere did I claim that "close to impossible" is a way of dealing with reliability issues. Rather, I said that (1) the intended purpose of TempData is for redirects (I have since confirmed this with a member of the MVC team directly), and (2) it's close to impossible to mess up TempData when doing a redirect, in non-pathological situations. The link you gave, and, in particular, the two comments from members of the MVC team at that link, support this conclusion.

Yes, it is hypothetically possible to mess this up. Hence, using it for something more critical than the display of an error message would be a mistake. But I haven't suggested that. Certainly, if someone had suggested passing credit card information, etc., this way, I would disagree with that. But that's a strawman here.

There is not a choice here between a "reliable" method and an "unreliable" method. We should not pretend that there is some "reliable" method of passing data across a redirect. You could put the data in the Session, but I would argue that the application is more likely to get it wrong this way than with TempData, which is built for the sole purpose. In other words, TempData is probably not perfect, but it is probably a better choice than the alternatives.</description>
		<content:encoded><![CDATA[<p><i>Craig, I’d rather disagree that "close to impossible" is an acceptable way of dealing with reliability issues.</i></p>
<p>Wait a second, George; you&#8217;re misquoting me.</p>
<p>Nowhere did I claim that "close to impossible" is a way of dealing with reliability issues. Rather, I said that (1) the intended purpose of TempData is for redirects (I have since confirmed this with a member of the MVC team directly), and (2) it&#8217;s close to impossible to mess up TempData when doing a redirect, in non-pathological situations. The link you gave, and, in particular, the two comments from members of the MVC team at that link, support this conclusion.</p>
<p>Yes, it is hypothetically possible to mess this up. Hence, using it for something more critical than the display of an error message would be a mistake. But I haven&#8217;t suggested that. Certainly, if someone had suggested passing credit card information, etc., this way, I would disagree with that. But that&#8217;s a strawman here.</p>
<p>There is not a choice here between a "reliable" method and an "unreliable" method. We should not pretend that there is some "reliable" method of passing data across a redirect. You could put the data in the Session, but I would argue that the application is more likely to get it wrong this way than with TempData, which is built for the sole purpose. In other words, TempData is probably not perfect, but it is probably a better choice than the alternatives.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: George</title>
		<link>http://blogs.teamb.com/craigstuntz/2009/01/23/37947/#comment-4440</link>
		<dc:creator>George</dc:creator>
		<pubDate>Mon, 02 Feb 2009 07:59:03 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.teamb.com/craigstuntz/?p=37947#comment-4440</guid>
		<description>Craig, I'd rather disagree that "close to impossible" is an acceptable way of dealing with reliability issues. This WILL happen sooner or later.

Here is the detailed analysis of the race condition:

http://stackoverflow.com/questions/235665/is-there-a-possible-race-condition-when-using-asp-net-mvc-tempdata-across-a-redir</description>
		<content:encoded><![CDATA[<p>Craig, I&#8217;d rather disagree that "close to impossible" is an acceptable way of dealing with reliability issues. This WILL happen sooner or later.</p>
<p>Here is the detailed analysis of the race condition:</p>
<p><a href="http://stackoverflow.com/questions/235665/is-there-a-possible-race-condition-when-using-asp-net-mvc-tempdata-across-a-redir" rel="nofollow">http://stackoverflow.com/questions/235665/is-there-a-possible-race-condition-when-using-asp-net-mvc-tempdata-across-a-redir</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Craig Stuntz</title>
		<link>http://blogs.teamb.com/craigstuntz/2009/01/23/37947/#comment-4362</link>
		<dc:creator>Craig Stuntz</dc:creator>
		<pubDate>Sat, 24 Jan 2009 01:58:46 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.teamb.com/craigstuntz/?p=37947#comment-4362</guid>
		<description>Xepol, it's close to impossible, in non-pathological situations, to mess up TempData &lt;i&gt;when doing a redirect&lt;/i&gt;, even with concurrent browser requests.

The docs about form data refer to a POST/redirect/GET scenario, which counts as "across requests" to me.</description>
		<content:encoded><![CDATA[<p>Xepol, it&#8217;s close to impossible, in non-pathological situations, to mess up TempData <i>when doing a redirect</i>, even with concurrent browser requests.</p>
<p>The docs about form data refer to a POST/redirect/GET scenario, which counts as "across requests" to me.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
