<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>entwicklungsgedanken &#187; ASP.Net MVC</title>
	<atom:link href="http://www.entwicklungsgedanken.de/category/asp-net-mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.entwicklungsgedanken.de</link>
	<description>Verschiedene Gedanken rund um die Softwareentwicklung</description>
	<lastBuildDate>Thu, 29 Jul 2010 11:09:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Redirect from the OnException-method inside the controller with ASP.Net MVC</title>
		<link>http://www.entwicklungsgedanken.de/2009/07/16/redirect-from-the-onexception-method-inside-the-controller-with-asp-net-mvc/</link>
		<comments>http://www.entwicklungsgedanken.de/2009/07/16/redirect-from-the-onexception-method-inside-the-controller-with-asp-net-mvc/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 08:42:00 +0000</pubDate>
		<dc:creator>Eric Bartels</dc:creator>
				<category><![CDATA[ASP.Net MVC]]></category>
		<category><![CDATA[Exception]]></category>
		<category><![CDATA[HandleError]]></category>
		<category><![CDATA[OnException]]></category>
		<category><![CDATA[redirect]]></category>

		<guid isPermaLink="false">http://www.entwicklungsgedanken.de/?p=290</guid>
		<description><![CDATA[If your controller uses the HandleError-Attribute it handles exceptions thrown by action methods. I wanted to catch these exceptions and redirect to a custom error page. A simple RedirectToAction inside the OnException method does not work. But the ExceptionContext is all we need. #region Error handling protected override void OnException&#40;ExceptionContext filterContext&#41; &#123; // Make use [...]]]></description>
			<content:encoded><![CDATA[<p>If your controller uses the <code>HandleError</code>-Attribute</code> it handles exceptions thrown by action methods. I wanted to catch these exceptions and redirect to a custom error page. A simple <code>RedirectToAction</code> inside the <code>OnException</code> method does not work. But the <code>ExceptionContext</code> is all we need.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080;">#region Error handling</span>
<span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnException<span style="color: #000000;">&#40;</span>ExceptionContext filterContext<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
 <span style="color: #008080; font-style: italic;">// Make use of the exception later</span>
 <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Session</span><span style="color: #000000;">&#91;</span><span style="color: #666666;">&quot;ErrorException&quot;</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">=</span> filterContext.<span style="color: #0000FF;">Exception</span><span style="color: #008000;">;</span>
&nbsp;
 <span style="color: #008080; font-style: italic;">// Mark exception as handled</span>
 filterContext.<span style="color: #0000FF;">ExceptionHandled</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
&nbsp;
 <span style="color: #008080; font-style: italic;">// ... logging, etc</span>
&nbsp;
 <span style="color: #008080; font-style: italic;">// Redirect</span>
 filterContext.<span style="color: #0000FF;">Result</span> <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">RedirectToAction</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Error&quot;</span>, <span style="color: #666666;">&quot;Home&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
 <span style="color: #0600FF;">base</span>.<span style="color: #0000FF;">OnException</span><span style="color: #000000;">&#40;</span>filterContext<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #008080;">#endregion</span></pre></div></div>

<p>It stored the exception thrown inside the action onto the session. So this information is available inside my "error-action".</p>
<p>My custom "error-action" even allows me to distinguish between normal requests and Ajax-requests (where I use a partial-view instead of a normal view) to display a nicer error message to the end-user.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.entwicklungsgedanken.de/2009/07/16/redirect-from-the-onexception-method-inside-the-controller-with-asp-net-mvc/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
