<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Strategies for resource-based 404 errors in ASP.NET MVC</title>
	<atom:link href="http://richarddingwall.name/2008/08/17/strategies-for-resource-based-404-errors-in-aspnet-mvc/feed/" rel="self" type="application/rss+xml" />
	<link>http://richarddingwall.name/2008/08/17/strategies-for-resource-based-404-errors-in-aspnet-mvc/</link>
	<description>The adventures of a young kiwi software developer in London</description>
	<lastBuildDate>Thu, 10 May 2012 16:20:46 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
	<item>
		<title>By: Mazlan</title>
		<link>http://richarddingwall.name/2008/08/17/strategies-for-resource-based-404-errors-in-aspnet-mvc/comment-page-1/#comment-27042</link>
		<dc:creator>Mazlan</dc:creator>
		<pubDate>Thu, 10 Mar 2011 02:39:28 +0000</pubDate>
		<guid isPermaLink="false">http://richarddingwall.name/?p=247#comment-27042</guid>
		<description>Hy see my answer for MVC 3

http://stackoverflow.com/questions/4985550/how-to-return-a-view-for-httpnotfound-in-asp-net-mvc-3/5254840#5254840</description>
		<content:encoded><![CDATA[<p>Hy see my answer for MVC 3</p>
<p><a href="http://stackoverflow.com/questions/4985550/how-to-return-a-view-for-httpnotfound-in-asp-net-mvc-3/5254840#5254840" rel="nofollow">http://stackoverflow.com/questions/4985550/how-to-return-a-view-for-httpnotfound-in-asp-net-mvc-3/5254840#5254840</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard</title>
		<link>http://richarddingwall.name/2008/08/17/strategies-for-resource-based-404-errors-in-aspnet-mvc/comment-page-1/#comment-3169</link>
		<dc:creator>Richard</dc:creator>
		<pubDate>Wed, 15 Apr 2009 23:27:28 +0000</pubDate>
		<guid isPermaLink="false">http://richarddingwall.name/?p=247#comment-3169</guid>
		<description>@AndrewO: there is one problem - Application_Error has no access to the current controller context - you cannot easily tell which Controller/Action is being executed, which is desired for nice ASP.NET MVC error logging :)</description>
		<content:encoded><![CDATA[<p>@AndrewO: there is one problem &#8211; Application_Error has no access to the current controller context &#8211; you cannot easily tell which Controller/Action is being executed, which is desired for nice ASP.NET MVC error logging :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AndrewO</title>
		<link>http://richarddingwall.name/2008/08/17/strategies-for-resource-based-404-errors-in-aspnet-mvc/comment-page-1/#comment-3010</link>
		<dc:creator>AndrewO</dc:creator>
		<pubDate>Sun, 12 Apr 2009 10:29:42 +0000</pubDate>
		<guid isPermaLink="false">http://richarddingwall.name/?p=247#comment-3010</guid>
		<description>Richard, thanks for your answer. I have some more questions.
In my webapplication I need:
1) as less code to perform errors as possible
2) write error description to log
3) return different result views for different errors:
3.1) error 404
 - show 404 page for page request
 - show just 404 status for images, css and js-files with no content
3.2) error 401
3.3) error 500
3.4) etc...

So, what do you think, if I:
1) set  in web.config (otherwise Application_Error doesn&#039;t fire in Global.asax)
2) put this code in Application_Error

        protected void Application_Error(object sender, EventArgs e)
        {
            Exception exception = Server.GetLastError();
            HttpException httpException = exception as HttpException;
            if (httpException != null)
            {
                RouteData routeData = new RouteData();
                routeData.Values.Add(&quot;controller&quot;, &quot;Error&quot;);
                routeData.Values.Add(&quot;action&quot;, &quot;HttpError500&quot;);
                if (httpException != null)
                {
                    if (httpException.GetHttpCode() == 404)
                    {
                        routeData.Values[&quot;action&quot;] = &quot;HttpError404&quot;;
                    }
                }
                Server.ClearError();
                Response.Clear();
                IController errorController = new ErrorController();
                errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
            }

        }
3) create ErrorController
        public ActionResult HttpError500()
        {
            return View(&quot;Error&quot;);
        }
        public ActionResult HttpError404()
        {
            Response.StatusCode = 404;
            return View(&quot;Error404&quot;);
        }


As you can see, I:
1) doesn&#039;t need to set any [HandleError] attributes
2) have one point to route exceptions
3) doesn&#039;t have problems with rule
routes.MapRoute(&quot;Default&quot;,
&quot;{controller}/{action}/{id}&quot;,                           
new { controller = &quot;Home&quot;, action = &quot;Index&quot;, id = &quot;&quot; });
4) can write to log when error occurred</description>
		<content:encoded><![CDATA[<p>Richard, thanks for your answer. I have some more questions.<br />
In my webapplication I need:<br />
1) as less code to perform errors as possible<br />
2) write error description to log<br />
3) return different result views for different errors:<br />
3.1) error 404<br />
 &#8211; show 404 page for page request<br />
 &#8211; show just 404 status for images, css and js-files with no content<br />
3.2) error 401<br />
3.3) error 500<br />
3.4) etc&#8230;</p>
<p>So, what do you think, if I:<br />
1) set  in web.config (otherwise Application_Error doesn&#8217;t fire in Global.asax)<br />
2) put this code in Application_Error</p>
<p>        protected void Application_Error(object sender, EventArgs e)<br />
        {<br />
            Exception exception = Server.GetLastError();<br />
            HttpException httpException = exception as HttpException;<br />
            if (httpException != null)<br />
            {<br />
                RouteData routeData = new RouteData();<br />
                routeData.Values.Add(&#8220;controller&#8221;, &#8220;Error&#8221;);<br />
                routeData.Values.Add(&#8220;action&#8221;, &#8220;HttpError500&#8243;);<br />
                if (httpException != null)<br />
                {<br />
                    if (httpException.GetHttpCode() == 404)<br />
                    {<br />
                        routeData.Values["action"] = &#8220;HttpError404&#8243;;<br />
                    }<br />
                }<br />
                Server.ClearError();<br />
                Response.Clear();<br />
                IController errorController = new ErrorController();<br />
                errorController.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));<br />
            }</p>
<p>        }<br />
3) create ErrorController<br />
        public ActionResult HttpError500()<br />
        {<br />
            return View(&#8220;Error&#8221;);<br />
        }<br />
        public ActionResult HttpError404()<br />
        {<br />
            Response.StatusCode = 404;<br />
            return View(&#8220;Error404&#8243;);<br />
        }</p>
<p>As you can see, I:<br />
1) doesn&#8217;t need to set any [HandleError] attributes<br />
2) have one point to route exceptions<br />
3) doesn&#8217;t have problems with rule<br />
routes.MapRoute(&#8220;Default&#8221;,<br />
&#8220;{controller}/{action}/{id}&#8221;,<br />
new { controller = &#8220;Home&#8221;, action = &#8220;Index&#8221;, id = &#8220;&#8221; });<br />
4) can write to log when error occurred</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard</title>
		<link>http://richarddingwall.name/2008/08/17/strategies-for-resource-based-404-errors-in-aspnet-mvc/comment-page-1/#comment-2710</link>
		<dc:creator>Richard</dc:creator>
		<pubDate>Sun, 05 Apr 2009 20:38:55 +0000</pubDate>
		<guid isPermaLink="false">http://richarddingwall.name/?p=247#comment-2710</guid>
		<description>@AndrewO: I think the benefit exceptions bring to your code are more important than the performance hit of using them (see http://www.yoda.arachsys.com/csharp/exceptions2.html). If it is a big problem though (e.g. someone is DDoS&#039;ing your site), you might try something like limiting requests if someone tries to access a non-existent resource 100 times or more.</description>
		<content:encoded><![CDATA[<p>@AndrewO: I think the benefit exceptions bring to your code are more important than the performance hit of using them (see <a href="http://www.yoda.arachsys.com/csharp/exceptions2.html" rel="nofollow">http://www.yoda.arachsys.com/csharp/exceptions2.html</a>). If it is a big problem though (e.g. someone is DDoS&#8217;ing your site), you might try something like limiting requests if someone tries to access a non-existent resource 100 times or more.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: AndrewO</title>
		<link>http://richarddingwall.name/2008/08/17/strategies-for-resource-based-404-errors-in-aspnet-mvc/comment-page-1/#comment-2698</link>
		<dc:creator>AndrewO</dc:creator>
		<pubDate>Sun, 05 Apr 2009 10:39:18 +0000</pubDate>
		<guid isPermaLink="false">http://richarddingwall.name/?p=247#comment-2698</guid>
		<description>Hello Richard!
Goods solution from one point, but don&#039;t you think that throwing Exception increase server workload?
Just think, what if someone calls your not found url 100 000 times...

So, I think liggett78 right and his solution is best.</description>
		<content:encoded><![CDATA[<p>Hello Richard!<br />
Goods solution from one point, but don&#8217;t you think that throwing Exception increase server workload?<br />
Just think, what if someone calls your not found url 100 000 times&#8230;</p>
<p>So, I think liggett78 right and his solution is best.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard</title>
		<link>http://richarddingwall.name/2008/08/17/strategies-for-resource-based-404-errors-in-aspnet-mvc/comment-page-1/#comment-2137</link>
		<dc:creator>Richard</dc:creator>
		<pubDate>Sat, 14 Feb 2009 04:20:42 +0000</pubDate>
		<guid isPermaLink="false">http://richarddingwall.name/?p=247#comment-2137</guid>
		<description>@mctip sure: in the HandleResourceNotFoundAttribute.OnException() method, where it sets up filterContext.Result. Something like:
&lt;pre&gt;        ViewDataDictionary viewData = controller.ViewData;
        viewData.Add(&quot;Url&quot;, filterContext.HttpContext.Request.Url);

        filterContext.Result = new ViewResult()
        {
            TempData = controller.TempData,
            ViewName = View,
            ViewData = viewData
        };&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>@mctip sure: in the HandleResourceNotFoundAttribute.OnException() method, where it sets up filterContext.Result. Something like:</p>
<pre>        ViewDataDictionary viewData = controller.ViewData;
        viewData.Add("Url", filterContext.HttpContext.Request.Url);

        filterContext.Result = new ViewResult()
        {
            TempData = controller.TempData,
            ViewName = View,
            ViewData = viewData
        };</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: mctip</title>
		<link>http://richarddingwall.name/2008/08/17/strategies-for-resource-based-404-errors-in-aspnet-mvc/comment-page-1/#comment-2135</link>
		<dc:creator>mctip</dc:creator>
		<pubDate>Fri, 13 Feb 2009 23:12:00 +0000</pubDate>
		<guid isPermaLink="false">http://richarddingwall.name/?p=247#comment-2135</guid>
		<description>Richard, is there a way to pass the invalid URL to the ResourceNotFound viewdata?</description>
		<content:encoded><![CDATA[<p>Richard, is there a way to pass the invalid URL to the ResourceNotFound viewdata?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: PK</title>
		<link>http://richarddingwall.name/2008/08/17/strategies-for-resource-based-404-errors-in-aspnet-mvc/comment-page-1/#comment-1335</link>
		<dc:creator>PK</dc:creator>
		<pubDate>Sat, 22 Nov 2008 13:23:49 +0000</pubDate>
		<guid isPermaLink="false">http://richarddingwall.name/?p=247#comment-1335</guid>
		<description>Cheers :)</description>
		<content:encoded><![CDATA[<p>Cheers :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Richard</title>
		<link>http://richarddingwall.name/2008/08/17/strategies-for-resource-based-404-errors-in-aspnet-mvc/comment-page-1/#comment-1323</link>
		<dc:creator>Richard</dc:creator>
		<pubDate>Sat, 22 Nov 2008 06:18:03 +0000</pubDate>
		<guid isPermaLink="false">http://richarddingwall.name/?p=247#comment-1323</guid>
		<description>PK: File uploaded, see the bottom of the article :)</description>
		<content:encoded><![CDATA[<p>PK: File uploaded, see the bottom of the article :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: PK</title>
		<link>http://richarddingwall.name/2008/08/17/strategies-for-resource-based-404-errors-in-aspnet-mvc/comment-page-1/#comment-1319</link>
		<dc:creator>PK</dc:creator>
		<pubDate>Sat, 22 Nov 2008 03:56:35 +0000</pubDate>
		<guid isPermaLink="false">http://richarddingwall.name/?p=247#comment-1319</guid>
		<description>Awesome. Before i posted my comment, i just hard coded in my &quot;ResourceNotFound&quot; view name, but i prefer your way because it&#039;s not tighly coupling it. 

Also looking forward to the beta 1 refresh. So far, i&#039;m LOVING this MVC stuff :) (being using it since Preview 2 :)</description>
		<content:encoded><![CDATA[<p>Awesome. Before i posted my comment, i just hard coded in my &#8220;ResourceNotFound&#8221; view name, but i prefer your way because it&#8217;s not tighly coupling it. </p>
<p>Also looking forward to the beta 1 refresh. So far, i&#8217;m LOVING this MVC stuff :) (being using it since Preview 2 :)</p>
]]></content:encoded>
	</item>
</channel>
</rss>

