<?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>Jan Jonas&#039; blog</title> <atom:link href="http://blog.janjonas.net/feed" rel="self" type="application/rss+xml" /><link>http://blog.janjonas.net</link> <description>Howtos &#38; Tutorials</description> <lastBuildDate>Thu, 02 Feb 2012 21:26:07 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>IIS 7.5 on Windows 7: How to fix &#8220;HTTP Error 401.3 &#8211; Unauthorized&#8221;</title><link>http://blog.janjonas.net/2012-02-02/iis_7_5-windows_7-how-to-http-error-401_3-unauthorized</link> <comments>http://blog.janjonas.net/2012-02-02/iis_7_5-windows_7-how-to-http-error-401_3-unauthorized#comments</comments> <pubDate>Thu, 02 Feb 2012 21:23:19 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Howto]]></category> <category><![CDATA[System administration]]></category> <category><![CDATA[HTTP]]></category> <category><![CDATA[IIS]]></category> <category><![CDATA[Windows]]></category> <guid
isPermaLink="false">http://blog.janjonas.net/?p=1493</guid> <description><![CDATA[If you try to run a web application on IIS 7.5 and Windows 7 and you get the following error HTTP Error 401.3 &#8211; Unauthorized You do not have permission to view this directory or page because of the access control list (ACL) configuration or encryption settings for this resource on the Web server. it [...]
Related posts:<ol><li><a
href='http://blog.janjonas.net/2010-08-25/microsoft-crm-4_0-fix-trysetfocusonsubject-error-installing-update-rollup-12' rel='bookmark' title='Microsoft CRM 4.0: Fix &#8220;TrySetFocusOnSubject&#8221; error after installing Update Rollup 12'>Microsoft CRM 4.0: Fix &#8220;TrySetFocusOnSubject&#8221; error after installing Update Rollup 12</a></li><li><a
href='http://blog.janjonas.net/2011-04-13/asp_net-prevent-iis_75_overriding-custom-error-page-iis-default-error-page' rel='bookmark' title='ASP.NET: Prevent IIS 7.5 from overriding custom error pages with IIS default error pages'>ASP.NET: Prevent IIS 7.5 from overriding custom error pages with IIS default error pages</a></li><li><a
href='http://blog.janjonas.net/2010-10-19/xenserver_5_6-debian-lenny-apt_get-aptitude-gpg-error' rel='bookmark' title='XenServer 5.6 &amp; Debian Lenny: Fix apt-get/aptitude GPG error'>XenServer 5.6 &#038; Debian Lenny: Fix apt-get/aptitude GPG error</a></li></ol>]]></description> <content:encoded><![CDATA[<p>If you try to run a web application on IIS 7.5 and Windows 7 and you get the following error</p><blockquote><p><strong>HTTP Error 401.3 &#8211; Unauthorized</strong><br
/> You do not have permission to view this directory or page because of the access control list (ACL) configuration or encryption settings for this resource on the Web server.</p></blockquote><p>it is most likely because of missing or wrong NTFS permissions. In detail, the identity that is used by the IIS web-server for &#8220;Anonymous Authentication&#8221; needs read permission on the web application&#8217;s physical folder. By default this identity is set to &#8220;<em>IUSR</em>&#8220;. To solve the HTTP Error 401.3 you could either</p><ul><li>Grant read permissions on the web application&#8217;s physical folder for the <em>IUSR</em> user or</li><li>change the identity that is used for &#8220;Anonymous Authentication&#8221; to a user that has the required read permissions. In order to change the identity use the IIS Manager, select the website, select &#8220;Authentication&#8221; (in section IIS), right click on &#8220;Anonymous Authentication&#8221;, run edit action and define a &#8220;Specific user&#8221;.</li></ul><p>Related posts:<ol><li><a
href='http://blog.janjonas.net/2010-08-25/microsoft-crm-4_0-fix-trysetfocusonsubject-error-installing-update-rollup-12' rel='bookmark' title='Microsoft CRM 4.0: Fix &#8220;TrySetFocusOnSubject&#8221; error after installing Update Rollup 12'>Microsoft CRM 4.0: Fix &#8220;TrySetFocusOnSubject&#8221; error after installing Update Rollup 12</a></li><li><a
href='http://blog.janjonas.net/2011-04-13/asp_net-prevent-iis_75_overriding-custom-error-page-iis-default-error-page' rel='bookmark' title='ASP.NET: Prevent IIS 7.5 from overriding custom error pages with IIS default error pages'>ASP.NET: Prevent IIS 7.5 from overriding custom error pages with IIS default error pages</a></li><li><a
href='http://blog.janjonas.net/2010-10-19/xenserver_5_6-debian-lenny-apt_get-aptitude-gpg-error' rel='bookmark' title='XenServer 5.6 &amp; Debian Lenny: Fix apt-get/aptitude GPG error'>XenServer 5.6 &#038; Debian Lenny: Fix apt-get/aptitude GPG error</a></li></ol></p><p><a
href="http://blog.janjonas.net/?flattrss_redirect&amp;id=1493&amp;md5=ebb7c18163ac71e87d9f3d6713aa5f25" title="Flattr" target="_blank"><img
src="http://blog.janjonas.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded> <wfw:commentRss>http://blog.janjonas.net/2012-02-02/iis_7_5-windows_7-how-to-http-error-401_3-unauthorized/feed</wfw:commentRss> <slash:comments>0</slash:comments> <atom:link rel="payment" href="http://blog.janjonas.net/?flattrss_redirect&amp;id=1493&amp;md5=ebb7c18163ac71e87d9f3d6713aa5f25" type="text/html" /> </item> <item><title>ASP.NET: Disable browser and proxy caching for aspx resources</title><link>http://blog.janjonas.net/2012-01-28/asp_net-disable-browser-proxy-caching-aspx-resources</link> <comments>http://blog.janjonas.net/2012-01-28/asp_net-disable-browser-proxy-caching-aspx-resources#comments</comments> <pubDate>Sat, 28 Jan 2012 15:45:50 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Howto]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[.NET]]></category> <category><![CDATA[ASP.NET]]></category> <category><![CDATA[HTTP]]></category> <guid
isPermaLink="false">http://blog.janjonas.net/?p=1482</guid> <description><![CDATA[The following lines of code can be put into a .aspx file to prevent the resource from being cached on clients or proxies: &#60;% Response.Cache.SetCacheability&#40;HttpCacheability.NoCache&#41;; Response.Cache.SetNoStore&#40;&#41;; %&#62; To prevent caching, these lines of code change the header of the server&#8217;s HTTP response by setting the following values: Cache-Control: no-cache, no-store Expires: -1 Pragma: no-cache You [...]
Related posts:<ol><li><a
href='http://blog.janjonas.net/2011-07-13/jquery-disable-browser-cache-all-ajax-requests' rel='bookmark' title='jQuery: Disable (browser) cache for all AJAX requests'>jQuery: Disable (browser) cache for all AJAX requests</a></li><li><a
href='http://blog.janjonas.net/2011-08-21/microsoft-iis-7-enable-client-side-browser-caching-static-content-web-config' rel='bookmark' title='Microsoft IIS 7: Enable Client Side Browser Caching for static Content in Web.config'>Microsoft IIS 7: Enable Client Side Browser Caching for static Content in Web.config</a></li><li><a
href='http://blog.janjonas.net/2011-12-28/asp_net-development-features-tips-tricks' rel='bookmark' title='ASP.NET development: Features, tips &amp; tricks you should know about'>ASP.NET development: Features, tips &#038; tricks you should know about</a></li></ol>]]></description> <content:encoded><![CDATA[<p>The following lines of code can be put into a <em>.aspx</em> file to prevent the resource from being cached on clients or proxies:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%</span>
Response<span style="color: #008000;">.</span><span style="color: #0000FF;">Cache</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SetCacheability</span><span style="color: #008000;">&#40;</span>HttpCacheability<span style="color: #008000;">.</span><span style="color: #0000FF;">NoCache</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
Response<span style="color: #008000;">.</span><span style="color: #0000FF;">Cache</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SetNoStore</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">%&gt;</span></pre></div></div><p>To prevent caching, these lines of code change the header of the server&#8217;s HTTP response by setting the following values:</p><ul><li><em>Cache-Control: no-cache, no-store</em></li><li><em>Expires: -1</em></li><li><em>Pragma: no-cache</em></li></ul><p>You can read more about other possible caching options that can be passed to <em>SetCacheability()</em> <a
href="http://msdn.microsoft.com/en-us/library/system.web.httpcacheability%28v=vs.80%29.aspx" target="_blank">here</a>.</p><p>Related posts:<ol><li><a
href='http://blog.janjonas.net/2011-07-13/jquery-disable-browser-cache-all-ajax-requests' rel='bookmark' title='jQuery: Disable (browser) cache for all AJAX requests'>jQuery: Disable (browser) cache for all AJAX requests</a></li><li><a
href='http://blog.janjonas.net/2011-08-21/microsoft-iis-7-enable-client-side-browser-caching-static-content-web-config' rel='bookmark' title='Microsoft IIS 7: Enable Client Side Browser Caching for static Content in Web.config'>Microsoft IIS 7: Enable Client Side Browser Caching for static Content in Web.config</a></li><li><a
href='http://blog.janjonas.net/2011-12-28/asp_net-development-features-tips-tricks' rel='bookmark' title='ASP.NET development: Features, tips &amp; tricks you should know about'>ASP.NET development: Features, tips &#038; tricks you should know about</a></li></ol></p><p><a
href="http://blog.janjonas.net/?flattrss_redirect&amp;id=1482&amp;md5=2c550443e2874e77e81fa07c040f56cf" title="Flattr" target="_blank"><img
src="http://blog.janjonas.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded> <wfw:commentRss>http://blog.janjonas.net/2012-01-28/asp_net-disable-browser-proxy-caching-aspx-resources/feed</wfw:commentRss> <slash:comments>0</slash:comments> <atom:link rel="payment" href="http://blog.janjonas.net/?flattrss_redirect&amp;id=1482&amp;md5=2c550443e2874e77e81fa07c040f56cf" type="text/html" /> </item> <item><title>ASP.NET MVC 3: Use re-mix library to add controller actions with mixins &#8211; Part 2</title><link>http://blog.janjonas.net/2012-01-15/asp_net-mvc_3-remix-library-add-controller-actions-mixins-part2</link> <comments>http://blog.janjonas.net/2012-01-15/asp_net-mvc_3-remix-library-add-controller-actions-mixins-part2#comments</comments> <pubDate>Sun, 15 Jan 2012 15:24:54 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Howto]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[.NET]]></category> <category><![CDATA[ASP.NET]]></category> <category><![CDATA[mixin]]></category> <category><![CDATA[MVC]]></category> <category><![CDATA[MVC 3]]></category> <category><![CDATA[re-mix]]></category> <guid
isPermaLink="false">http://blog.janjonas.net/?p=1460</guid> <description><![CDATA[This is an update to my previous post &#8220;ASP.NET MVC 3: Use re-mix library to add controller actions with mixins&#8221;. In November last year I implemented a prototypical approach that uses mixins to add actions to ASP.NET MVC 3 controllers. Since .NET does not support the mixin concept out of the box, I used the [...]
Related posts:<ol><li><a
href='http://blog.janjonas.net/2011-11-26/asp_net-mvc_3-remix-library-add-controller-actions-mixins' rel='bookmark' title='ASP.NET MVC 3: Use re-mix library to add controller actions with mixins'>ASP.NET MVC 3: Use re-mix library to add controller actions with mixins</a></li><li><a
href='http://blog.janjonas.net/2011-06-18/aspnet-mvc3-controller-extension-method-render-partial-view-string' rel='bookmark' title='ASP.NET MVC3: Controller extension methods to render a partial view to string'>ASP.NET MVC3: Controller extension methods to render a partial view to string</a></li><li><a
href='http://blog.janjonas.net/2011-02-15/aspnet-mvc_3-get-controller-action-razor-view' rel='bookmark' title='ASP.NET MVC 3: How to get the current controller and action in a Razor view'>ASP.NET MVC 3: How to get the current controller and action in a Razor view</a></li></ol>]]></description> <content:encoded><![CDATA[<p>This is an update to my previous post <a
href="http://blog.janjonas.net/2011-11-26/asp_net-mvc_3-remix-library-add-controller-actions-mixins">&#8220;ASP.NET MVC 3: Use re-mix library to add controller actions with mixins&#8221;</a>. In November last year I implemented a prototypical approach that uses <a
href="http://en.wikipedia.org/wiki/Mixins" target="_blank">mixins</a> to add actions to ASP.NET MVC 3 controllers. Since .NET does not support the mixin concept out of the box, I used the <a
href="http://remix.codeplex.com/" target="_blank">re-mix</a> library in this implementation.</p><p>Even though my implementation basically worked, there were several limitations (listed at the end of the <a
href="http://blog.janjonas.net/2011-11-26/asp_net-mvc_3-remix-library-add-controller-actions-mixins">blog post</a>) that hinders using this approach in a real world project. Thanks to Fabian Schmied and his blog post <a
href="https://www.re-motion.org/blogs/mix/2012/01/10/re-mix-encapsulate-and-share-asp-net-mvc-controller-actions/" target="_blank">&#8220;re-mix: Encapsulate and share ASP.NET MVC controller actions&#8221;</a> it turned out that I was not aware of all the cool re-mix  features that could solve the problems.</p><p>In this blog post I will show how to use Fabian&#8217;s suggestions to improve my first implementation. Summarized, his suggestions lead to a very a smooth integration of the re-mix library into a ASP.NET MVC 3 web application that enables adding actions to controllers without complex inheritance structures.</p><p>To understand the steps described below in detail it could be reasonable to download the <a
href="http://blog.janjonas.net/wp-content/uploads/2011/11/vs_2010-mvc-remix.zip" target="_blank">source code</a> of my first implementation. At the end of this post you find the download link for the updated version that includes all improvements.</p><h2>Use <em>IntroducedMemberVisibility = MemberVisibility.Public</em></h2><p>When adding the <em>Uses</em> attribute to the controllers one could set <em>IntroducedMemberVisibility = MemberVisibility.Public</em> to tell re-mix to implement interface members publicly. As a result there is no need for the custom <em>ControllerActionInvoker</em>; the default ASP.NET MVC 3 <em>ControllerActionInvoker</em> will now find the actions defined in the mixins as if there were defined directly in the controller (like &#8220;normal&#8221; actions).<br
/> Instead of setting the option every time the remix <em>UsesAttribute</em> is used to annotate a controller, one could (as Fabian suggested) define a custom <em>ControllerUsesMixinAttribute</em> that takes care of setting the option:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ControllerUsesMixinAttribute <span style="color: #008000;">:</span> UsesAttribute
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> ControllerUsesMixinAttribute<span style="color: #008000;">&#40;</span>Type mixinType<span style="color: #008000;">&#41;</span> <span style="color: #008000;">:</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">&#40;</span>mixinType<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    IntroducedMemberVisibility <span style="color: #008000;">=</span> MemberVisibility<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Public</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>and use this attribute to annotate the controllers:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>ControllerUsesMixinAttribute<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>EchoControllerMixin<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Controller1Controller <span style="color: #008000;">:</span> Controller
<span style="color: #008000;">&#123;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><h2>Defined a <em>IMixedController</em> interface</h2><p>To access the protected method defined in the default ASP.NET MVC 3 <em><a
href="http://msdn.microsoft.com/en-us/library/system.web.mvc.controllerbase.aspx" target="_blank">ControllerBase</a></em> class one could specify a custom interface <em>IMixedController</em> like</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">interface</span> IMixedController
<span style="color: #008000;">&#123;</span>
  ContentResult Content<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> content<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  ControllerContext ControllerContext <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>and use this interface as <em>TTarget</em> type when defining the mixin class:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> EchoControllerMixin <span style="color: #008000;">:</span> Mixin<span style="color: #008000;">&lt;</span>IMixedController<span style="color: #008000;">&gt;</span>, IEchoControllerMixin
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> ActionResult Echo<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> id<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    var controllerName <span style="color: #008000;">=</span> Target<span style="color: #008000;">.</span><span style="color: #0000FF;">ControllerContext</span><span style="color: #008000;">.</span><span style="color: #0000FF;">RouteData</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Values</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;controller&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> Target<span style="color: #008000;">.</span><span style="color: #0000FF;">Content</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;re-mix added 'Echo' action to controller '{0}'&lt;hr/&gt;You said: {1}&quot;</span>, controllerName, <span style="color: #008000;">!</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #008000;">&#40;</span>id<span style="color: #008000;">&#41;</span> <span style="color: #008000;">?</span> id <span style="color: #008000;">:</span> <span style="color: #666666;">&quot;&lt;i&gt;nothing&lt;/i&gt;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>Of course there are many more methods and properties defined in the <em>ControllerBase</em> class that could be added to the <em>IMixedController</em> interface.</p><h2><em>RemixControllerFactory</em>: Subclass <em>DefaultControllerFactory</em> instead of implementing <em>IControllerFactory</em></h2><p>The custom controller factory <em>RemixControllerFactory</em> is responsible for creating the concrete controller instance (which might be a re-mix proxy object if the controller has any <em>UsesAttribute</em>s). In my first implementation I used ASP.NET MVC <em>DefaultControllerFactory</em> to create the controller object (without the &#8220;intermixed&#8221; actions) to get the type of the controller that should be used. In a second step, this type was passed to re-mix&#8217;s <em>ObjectFactory</em> to create the &#8220;correct&#8221; controller (containing the &#8220;intermixed&#8221; actions). The drawback of this approach is that the controller object is created twice.</p><p>A much better way is to subclass <em>DefaultControllerFactory</em> and override <em>GetControllerType()</em>:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> RemixControllerFactory <span style="color: #008000;">:</span> DefaultControllerFactory
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> Type GetControllerType<span style="color: #008000;">&#40;</span>RequestContext requestContext, <span style="color: #6666cc; font-weight: bold;">string</span> controllerName<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    var baseControllerType <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetControllerType</span><span style="color: #008000;">&#40;</span>requestContext, controllerName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> Remotion<span style="color: #008000;">.</span><span style="color: #0000FF;">Mixins</span><span style="color: #008000;">.</span><span style="color: #0000FF;">TypeFactory</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetConcreteType</span><span style="color: #008000;">&#40;</span>baseControllerType<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>I updated the source code and the Visual Studio 2010 project, you can download the new version <a
href="http://blog.janjonas.net/wp-content/uploads/2012/01/vs_2010-mvc-remix-part2.zip" target="_blank">here</a>.</p><p>Related posts:<ol><li><a
href='http://blog.janjonas.net/2011-11-26/asp_net-mvc_3-remix-library-add-controller-actions-mixins' rel='bookmark' title='ASP.NET MVC 3: Use re-mix library to add controller actions with mixins'>ASP.NET MVC 3: Use re-mix library to add controller actions with mixins</a></li><li><a
href='http://blog.janjonas.net/2011-06-18/aspnet-mvc3-controller-extension-method-render-partial-view-string' rel='bookmark' title='ASP.NET MVC3: Controller extension methods to render a partial view to string'>ASP.NET MVC3: Controller extension methods to render a partial view to string</a></li><li><a
href='http://blog.janjonas.net/2011-02-15/aspnet-mvc_3-get-controller-action-razor-view' rel='bookmark' title='ASP.NET MVC 3: How to get the current controller and action in a Razor view'>ASP.NET MVC 3: How to get the current controller and action in a Razor view</a></li></ol></p><p><a
href="http://blog.janjonas.net/?flattrss_redirect&amp;id=1460&amp;md5=32b36cbdbde8ca27975f436e386d06ff" title="Flattr" target="_blank"><img
src="http://blog.janjonas.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded> <wfw:commentRss>http://blog.janjonas.net/2012-01-15/asp_net-mvc_3-remix-library-add-controller-actions-mixins-part2/feed</wfw:commentRss> <slash:comments>1</slash:comments> <atom:link rel="payment" href="http://blog.janjonas.net/?flattrss_redirect&amp;id=1460&amp;md5=32b36cbdbde8ca27975f436e386d06ff" type="text/html" /> </item> <item><title>ASP.NET MVC 3: Support multiple submit buttons in one form with HTML Helper MultiSubmitButton</title><link>http://blog.janjonas.net/2012-01-04/asp_net-mvc_3-multiple-submit-buttons-one-form-html-helper-multisubmitbutton</link> <comments>http://blog.janjonas.net/2012-01-04/asp_net-mvc_3-multiple-submit-buttons-one-form-html-helper-multisubmitbutton#comments</comments> <pubDate>Wed, 04 Jan 2012 13:38:36 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Howto]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[.NET]]></category> <category><![CDATA[ASP.NET]]></category> <category><![CDATA[MVC]]></category> <category><![CDATA[MVC 3]]></category> <guid
isPermaLink="false">http://blog.janjonas.net/?p=1434</guid> <description><![CDATA[Out of the box ASP.NET MVC 3 only supports one submit button for each form. To be more precise: The MVC 3 framework only supports one submit action for each form, i.e. all submit buttons in the form post the data to the same URL (the URL that is defined in the action attribute of [...]
Related posts:<ol><li><a
href='http://blog.janjonas.net/2011-08-07/asp_net-mvc_3-jquery-ajax-submit-ajax-form-supporting-unobtrusive-client-side-validation-and-server-side-validation' rel='bookmark' title='ASP.NET MVC 3: Using jQuery .ajax() function to submit Ajax Form supporting (unobtrusive) Client Side Validation and Server Side Validation'>ASP.NET MVC 3: Using jQuery .ajax() function to submit Ajax Form supporting (unobtrusive) Client Side Validation and Server Side Validation</a></li><li><a
href='http://blog.janjonas.net/2011-10-23/asp_net-mvc_3-strongly-typed-authorize-attribute-multiple-users-roles' rel='bookmark' title='ASP.NET MVC 3: Strongly typed Authorize Attribute with multiple users and roles'>ASP.NET MVC 3: Strongly typed Authorize Attribute with multiple users and roles</a></li><li><a
href='http://blog.janjonas.net/2011-07-24/asp_net-mvc_3-ajax-form-jquery-validate-supporting-unobtrusive-client-side-validation-and-server-side-validation' rel='bookmark' title='ASP.NET MVC 3: Ajax Form with jQuery validate supporting (unobtrusive) Client Side Validation and Server Side Validation'>ASP.NET MVC 3: Ajax Form with jQuery validate supporting (unobtrusive) Client Side Validation and Server Side Validation</a></li></ol>]]></description> <content:encoded><![CDATA[<p>Out of the box ASP.NET MVC 3 only supports one submit button for each form. To be more precise: The MVC 3 framework only supports one submit action for each form, i.e. all submit buttons in the form post the data to the same URL (the URL that is defined in the <em>action</em> attribute of the the <em>&lt;form&gt;</em> tag).</p><p>One scenario where more than one submit button is useful is for example a form that enables the user to update a dataset or to save the data as a new dataset. On the server side, one would implement two actions (like <em>Update()</em> and <em>AddNew()</em>) and requires a logic that decides which actions should be executed depending on which button was clicked.</p><p>There are approaches that solve this problem by decorate the actions with additional attributes:</p><ul><li><a
href="http://blog.maartenballiauw.be/post/2009/11/26/Supporting-multiple-submit-buttons-on-an-ASPNET-MVC-view.aspx" target="_blank">http://blog.maartenballiauw.be/post/2009/11/26/Supporting-multiple-submit-buttons-on-an-ASPNET-MVC-view.aspx</a></li><li><a
href="http://blog.ashmind.com/2010/03/15/multiple-submit-buttons-with-asp-net-mvc-final-solution" target="_blank">http://blog.ashmind.com/2010/03/15/multiple-submit-buttons-with-asp-net-mvc-final-solution</a></li></ul><p>The basic idea of these approaches is to set the <em>name</em> attribute of the submit button&#8217;s <em>&lt;input&gt;</em> tag and implement a custom <em>ActionNameSelectorAttribute</em> that tests the submitted data of the HTTP request against this value to decide whether the action should be executed or not.</p><p>In this blog post I will show how to implement a custom HTML Helper that supports creating forms with multiple submit buttons. The benefit of this approach is that no changes on the server side  (like additional attributes) are required. The drawback of this solution is that it depends on JavaScript (jQuery), i.e. it only works in browsers with JavaScript support enabled.</p><p>The HTML Helper <em>MultiSubmitButton</em> shown below depends on the <em>SubmitButton()</em> HTML Helper of the <a
href="http://aspnet.codeplex.com/releases/view/58781" target="_blank">ASP.NET MVC 3 Futures</a> library. One could use the <a
href="http://nuget.codeplex.com/" target="_blank">NuGet package manager</a> to install MVC 3 Futures</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;">PM<span style="color: #008000;">&gt;</span> Install<span style="color: #008000;">-</span>Package Mvc3Futures</pre></div></div><p>or download the files <a
href="http://aspnet.codeplex.com/releases/view/58781#DownloadId=211128" target="_blank">here</a>.</p><p>The <em>MultiSubmitButton</em> HTML Helper works like the MVC 3 Futures&#8217; <em>SubmitButton</em> HTML Helper but has an additional (string) parameter <em>url</em> which defines the URL of the action that should be called when the button was clicked. The implementation looks as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> MultiSubmitButtonExtension
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> MvcHtmlString MultiSubmitButton<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> HtmlHelper helper, <span style="color: #6666cc; font-weight: bold;">string</span> url, <span style="color: #6666cc; font-weight: bold;">string</span> name<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> MultiSubmitButton<span style="color: #008000;">&#40;</span>helper, url, name, <span style="color: #0600FF; font-weight: bold;">null</span>, <span style="color: #008000;">&#40;</span>IDictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#41;</span><span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> MvcHtmlString MultiSubmitButton<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> HtmlHelper helper, <span style="color: #6666cc; font-weight: bold;">string</span> url, <span style="color: #6666cc; font-weight: bold;">string</span> name, <span style="color: #6666cc; font-weight: bold;">string</span> buttonText<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> MultiSubmitButton<span style="color: #008000;">&#40;</span>helper, url, name, buttonText, <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> MvcHtmlString MultiSubmitButton<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> HtmlHelper helper, <span style="color: #6666cc; font-weight: bold;">string</span> url<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> MultiSubmitButton<span style="color: #008000;">&#40;</span>helper, url, <span style="color: #0600FF; font-weight: bold;">null</span>, <span style="color: #0600FF; font-weight: bold;">null</span>, <span style="color: #008000;">&#40;</span>IDictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#41;</span><span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> MvcHtmlString MultiSubmitButton<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> HtmlHelper helper, <span style="color: #6666cc; font-weight: bold;">string</span> url, <span style="color: #6666cc; font-weight: bold;">string</span> name, <span style="color: #6666cc; font-weight: bold;">string</span> buttonText, <span style="color: #6666cc; font-weight: bold;">object</span> htmlAttributes<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> helper<span style="color: #008000;">.</span><span style="color: #0000FF;">MultiSubmitButton</span><span style="color: #008000;">&#40;</span>url, name, buttonText, HtmlHelper<span style="color: #008000;">.</span><span style="color: #0000FF;">AnonymousObjectToHtmlAttributes</span><span style="color: #008000;">&#40;</span>htmlAttributes<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> MvcHtmlString MultiSubmitButton<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span> HtmlHelper helper, <span style="color: #6666cc; font-weight: bold;">string</span> url, <span style="color: #6666cc; font-weight: bold;">string</span> name, <span style="color: #6666cc; font-weight: bold;">string</span> buttonText, IDictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&gt;</span> htmlAttributes<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    htmlAttributes <span style="color: #008000;">=</span> htmlAttributes <span style="color: #008000;">??</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span>, <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Add onClick handler</span>
    <span style="color: #6666cc; font-weight: bold;">string</span> onClick <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;$(this).parents('form').attr('action', '&quot;</span> <span style="color: #008000;">+</span> url <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;');&quot;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>htmlAttributes<span style="color: #008000;">.</span><span style="color: #0000FF;">ContainsKey</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;onClick&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
      htmlAttributes<span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;onClick&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> htmlAttributes<span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;onClick&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">+</span> <span style="color: #666666;">&quot;;&quot;</span> <span style="color: #008000;">+</span> onClick<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #008000;">&#123;</span>
      htmlAttributes<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;onClick&quot;</span>, onClick<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
   <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> helper<span style="color: #008000;">.</span><span style="color: #0000FF;">SubmitButton</span><span style="color: #008000;">&#40;</span>name, buttonText, htmlAttributes<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>You can combine the <em>MultiSubmitButton</em> HTML Helper with ordinary submit buttons: The multi submit buttons will call the action that is defined with its <em>url</em> parameter and the ordinary submit buttons will call the form&#8217;s default action (defined in the form&#8217;s <em>action</em> attribute).</p><p>Here is an example how to use the <em>MultiSubmitButton</em> HTML Helper:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;">@<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>Html<span style="color: #008000;">.</span><span style="color: #0000FF;">BeginForm</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;DefaultAction&quot;</span>, <span style="color: #666666;">&quot;Controller&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
&nbsp;
  <span style="color: #008080; font-style: italic;">// Add input fields here ...</span>
&nbsp;
  @Html<span style="color: #008000;">.</span><span style="color: #0000FF;">SubmitButton</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;button1&quot;</span>, <span style="color: #666666;">&quot;Default Action&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  @Html<span style="color: #008000;">.</span><span style="color: #0000FF;">MultiSubmitButton</span><span style="color: #008000;">&#40;</span>Url<span style="color: #008000;">.</span><span style="color: #0000FF;">Action</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;FormAction1&quot;</span><span style="color: #008000;">&#41;</span>, <span style="color: #666666;">&quot;button2&quot;</span>, <span style="color: #666666;">&quot;Action 1&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  @Html<span style="color: #008000;">.</span><span style="color: #0000FF;">MultiSubmitButton</span><span style="color: #008000;">&#40;</span>Url<span style="color: #008000;">.</span><span style="color: #0000FF;">Action</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;FormAction2&quot;</span><span style="color: #008000;">&#41;</span>, <span style="color: #666666;">&quot;button3&quot;</span>, <span style="color: #666666;">&quot;Action 2&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008000;">&#125;</span></pre></div></div><p>As you can see in the example above one could use the <em>Url.Action()</em> method to easily generate the URL that is passed to the <em>MultiSubmitButton</em> helper.</p><p>A running Visual Studio 2010 project containing all the source code could be downloaded <a
href="http://blog.janjonas.net/wp-content/uploads/2012/01/vs_2010-mvc3-multi-submit-buttons-in-one-form.zip" target="_blank">here</a>.</p><p>Related posts:<ol><li><a
href='http://blog.janjonas.net/2011-08-07/asp_net-mvc_3-jquery-ajax-submit-ajax-form-supporting-unobtrusive-client-side-validation-and-server-side-validation' rel='bookmark' title='ASP.NET MVC 3: Using jQuery .ajax() function to submit Ajax Form supporting (unobtrusive) Client Side Validation and Server Side Validation'>ASP.NET MVC 3: Using jQuery .ajax() function to submit Ajax Form supporting (unobtrusive) Client Side Validation and Server Side Validation</a></li><li><a
href='http://blog.janjonas.net/2011-10-23/asp_net-mvc_3-strongly-typed-authorize-attribute-multiple-users-roles' rel='bookmark' title='ASP.NET MVC 3: Strongly typed Authorize Attribute with multiple users and roles'>ASP.NET MVC 3: Strongly typed Authorize Attribute with multiple users and roles</a></li><li><a
href='http://blog.janjonas.net/2011-07-24/asp_net-mvc_3-ajax-form-jquery-validate-supporting-unobtrusive-client-side-validation-and-server-side-validation' rel='bookmark' title='ASP.NET MVC 3: Ajax Form with jQuery validate supporting (unobtrusive) Client Side Validation and Server Side Validation'>ASP.NET MVC 3: Ajax Form with jQuery validate supporting (unobtrusive) Client Side Validation and Server Side Validation</a></li></ol></p><p><a
href="http://blog.janjonas.net/?flattrss_redirect&amp;id=1434&amp;md5=cc705cf88729d3a6198e89cae2cc84e4" title="Flattr" target="_blank"><img
src="http://blog.janjonas.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded> <wfw:commentRss>http://blog.janjonas.net/2012-01-04/asp_net-mvc_3-multiple-submit-buttons-one-form-html-helper-multisubmitbutton/feed</wfw:commentRss> <slash:comments>0</slash:comments> <atom:link rel="payment" href="http://blog.janjonas.net/?flattrss_redirect&amp;id=1434&amp;md5=cc705cf88729d3a6198e89cae2cc84e4" type="text/html" /> </item> <item><title>ASP.NET MVC 3: Async jQuery progress indicator for long running tasks</title><link>http://blog.janjonas.net/2012-01-02/asp_net-mvc_3-async-jquery-progress-indicator-long-running-tasks</link> <comments>http://blog.janjonas.net/2012-01-02/asp_net-mvc_3-async-jquery-progress-indicator-long-running-tasks#comments</comments> <pubDate>Mon, 02 Jan 2012 13:29:41 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Howto]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[.NET]]></category> <category><![CDATA[AJAX]]></category> <category><![CDATA[ASP.NET]]></category> <category><![CDATA[jQuery]]></category> <category><![CDATA[MVC]]></category> <category><![CDATA[MVC 3]]></category> <guid
isPermaLink="false">http://blog.janjonas.net/?p=1417</guid> <description><![CDATA[If you have long running (server side) tasks in your ASP.NET MVC 3 web application it could be reasonable to provide the user with information about the current progress of this task. In this blog post I will show how such a progress indicator could be implemented using jQuery and AJAX. The basic idea is [...]
Related posts:<ol><li><a
href='http://blog.janjonas.net/2011-08-07/asp_net-mvc_3-jquery-ajax-submit-ajax-form-supporting-unobtrusive-client-side-validation-and-server-side-validation' rel='bookmark' title='ASP.NET MVC 3: Using jQuery .ajax() function to submit Ajax Form supporting (unobtrusive) Client Side Validation and Server Side Validation'>ASP.NET MVC 3: Using jQuery .ajax() function to submit Ajax Form supporting (unobtrusive) Client Side Validation and Server Side Validation</a></li><li><a
href='http://blog.janjonas.net/2011-08-09/asp_net-mvc_3-json-result-jquery-ajax-form-validation' rel='bookmark' title='ASP.NET MVC 3: Using JSON result for jQuery Ajax Forms validation'>ASP.NET MVC 3: Using JSON result for jQuery Ajax Forms validation</a></li><li><a
href='http://blog.janjonas.net/2011-07-24/asp_net-mvc_3-ajax-form-jquery-validate-supporting-unobtrusive-client-side-validation-and-server-side-validation' rel='bookmark' title='ASP.NET MVC 3: Ajax Form with jQuery validate supporting (unobtrusive) Client Side Validation and Server Side Validation'>ASP.NET MVC 3: Ajax Form with jQuery validate supporting (unobtrusive) Client Side Validation and Server Side Validation</a></li></ol>]]></description> <content:encoded><![CDATA[<p>If you have long running (server side) tasks in your ASP.NET MVC 3 web application it could be reasonable to provide the user with information about the current progress of this task. In this blog post I will show how such a progress indicator could be implemented using jQuery and AJAX.</p><p>The basic idea is as follows: Two actions exists on the server side; one action starts the (long running) task in a new thread and returns a unique identifier (GUID in this example) to the client and the second action returns the current (percentaged) status for a specific task.</p><p>For simplicity, in this example all (server side) functionality is implemented in the <em>HomeController</em>:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> HomeController <span style="color: #008000;">:</span> Controller
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #0600FF; font-weight: bold;">static</span> IDictionary<span style="color: #008000;">&lt;</span>Guid, <span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span> tasks <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Dictionary<span style="color: #008000;">&lt;</span>Guid, <span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> ActionResult Index<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> View<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> ActionResult Start<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    var taskId <span style="color: #008000;">=</span> Guid<span style="color: #008000;">.</span><span style="color: #0000FF;">NewGuid</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    tasks<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span>taskId, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    Task<span style="color: #008000;">.</span><span style="color: #0000FF;">Factory</span><span style="color: #008000;">.</span><span style="color: #0000FF;">StartNew</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span>
    <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">for</span> <span style="color: #008000;">&#40;</span>var i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;=</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
        tasks<span style="color: #008000;">&#91;</span>taskId<span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> i<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// update task progress</span>
        Thread<span style="color: #008000;">.</span><span style="color: #0000FF;">Sleep</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">50</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// simulate long running operation</span>
      <span style="color: #008000;">&#125;</span>
      tasks<span style="color: #008000;">.</span><span style="color: #0000FF;">Remove</span><span style="color: #008000;">&#40;</span>taskId<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">return</span> Json<span style="color: #008000;">&#40;</span>taskId<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> ActionResult Progress<span style="color: #008000;">&#40;</span>Guid id<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> Json<span style="color: #008000;">&#40;</span>tasks<span style="color: #008000;">.</span><span style="color: #0000FF;">Keys</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Contains</span><span style="color: #008000;">&#40;</span>id<span style="color: #008000;">&#41;</span> <span style="color: #008000;">?</span> tasks<span style="color: #008000;">&#91;</span>id<span style="color: #008000;">&#93;</span> <span style="color: #008000;">:</span> <span style="color: #FF0000;">100</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>The <em>HomeController</em> uses a Dictionary to save the progresses of all tasks. The <em>Start</em> action adds a new entry to this dictionary (with progress 0) and starts a new <em>Task</em> that simulates the long running operation. It is important to understand that this (time-consuming) operation is running in a separate thread and server immediately returns a result to the client (the unique identifier of the task), i.e. there is no HTTP request that is open as long as the task runs. The <em>Progress</em> action just returns the progress for a given GUID that uniquely identifies a task. Both actions return a JSON-result to enable easily processing of the data on the client side.</p><p>The client side functionality is implemented in the <em>Index.cshtml</em> view:</p><div
class="wp_syntax"><div
class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> updateMonitor<span style="color: #009900;">&#40;</span>taskId<span style="color: #339933;">,</span> <span style="color: #000066;">status</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#&quot;</span> <span style="color: #339933;">+</span> taskId<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Task [&quot;</span> <span style="color: #339933;">+</span> taskId <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;]: &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066;">status</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#start&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    e.<span style="color: #660066;">preventDefault</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    $.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Home/Start&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>taskId<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// Init monitors</span>
      $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#monitors&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;p id='&quot;</span> <span style="color: #339933;">+</span> taskId <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;'/&gt;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      updateMonitor<span style="color: #009900;">&#40;</span>taskId<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Started&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// Periodically update monitors</span>
      <span style="color: #003366; font-weight: bold;">var</span> intervalId <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $.<span style="color: #660066;">post</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Home/Progress&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> id<span style="color: #339933;">:</span> taskId <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>progress<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>progress <span style="color: #339933;">&gt;=</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            updateMonitor<span style="color: #009900;">&#40;</span>taskId<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;Completed&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          clearInterval<span style="color: #009900;">&#40;</span>intervalId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            updateMonitor<span style="color: #009900;">&#40;</span>taskId<span style="color: #339933;">,</span> progress <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;%&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
&nbsp;
&lt;div id=&quot;monitors&quot;&gt;&lt;/div&gt;</pre></div></div><p>On the client side the jQuery function <em>$.post()</em> is used to</p><ul><li>first &#8220;call&#8221; the URL <em>Home/Start</em> to start a new task</li><li>and then periodically pass the returned GUID to the URL <em>Home/Progress</em> to get the current progress.</li></ul><p>To show some feedback to the user a <em>&lt;p&gt;</em> tag is created for each started task and periodically updated with the current progress.</p><p>Note: The approach shown above supports multiple parallel running tasks. You can click the &#8220;start&#8221; link while other task(s) are still running.</p><p>The code shown above is only exemplary. In a real-world project one should implement a robust error handling to support for example situations where the tasks are interrupted. Furthermore it could be reasonable to limit the number of parallel tasks one user could start, since long running tasks are often need much resources.</p><p>You can download the Visual Studio 2010 project containing all the source code <a
href="http://blog.janjonas.net/wp-content/uploads/2012/01/vs_2010-mvc-jquery-async-progress-indicator.zip">here</a>.</p><p>Related posts:<ol><li><a
href='http://blog.janjonas.net/2011-08-07/asp_net-mvc_3-jquery-ajax-submit-ajax-form-supporting-unobtrusive-client-side-validation-and-server-side-validation' rel='bookmark' title='ASP.NET MVC 3: Using jQuery .ajax() function to submit Ajax Form supporting (unobtrusive) Client Side Validation and Server Side Validation'>ASP.NET MVC 3: Using jQuery .ajax() function to submit Ajax Form supporting (unobtrusive) Client Side Validation and Server Side Validation</a></li><li><a
href='http://blog.janjonas.net/2011-08-09/asp_net-mvc_3-json-result-jquery-ajax-form-validation' rel='bookmark' title='ASP.NET MVC 3: Using JSON result for jQuery Ajax Forms validation'>ASP.NET MVC 3: Using JSON result for jQuery Ajax Forms validation</a></li><li><a
href='http://blog.janjonas.net/2011-07-24/asp_net-mvc_3-ajax-form-jquery-validate-supporting-unobtrusive-client-side-validation-and-server-side-validation' rel='bookmark' title='ASP.NET MVC 3: Ajax Form with jQuery validate supporting (unobtrusive) Client Side Validation and Server Side Validation'>ASP.NET MVC 3: Ajax Form with jQuery validate supporting (unobtrusive) Client Side Validation and Server Side Validation</a></li></ol></p><p><a
href="http://blog.janjonas.net/?flattrss_redirect&amp;id=1417&amp;md5=c1174e10f9550945fefc48230b5c5cff" title="Flattr" target="_blank"><img
src="http://blog.janjonas.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded> <wfw:commentRss>http://blog.janjonas.net/2012-01-02/asp_net-mvc_3-async-jquery-progress-indicator-long-running-tasks/feed</wfw:commentRss> <slash:comments>0</slash:comments> <atom:link rel="payment" href="http://blog.janjonas.net/?flattrss_redirect&amp;id=1417&amp;md5=c1174e10f9550945fefc48230b5c5cff" type="text/html" /> </item> <item><title>ASP.NET development: Features, tips &amp; tricks you should know about</title><link>http://blog.janjonas.net/2011-12-28/asp_net-development-features-tips-tricks</link> <comments>http://blog.janjonas.net/2011-12-28/asp_net-development-features-tips-tricks#comments</comments> <pubDate>Wed, 28 Dec 2011 15:33:46 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Howto]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[.NET]]></category> <category><![CDATA[ASP.NET]]></category> <guid
isPermaLink="false">http://blog.janjonas.net/?p=1394</guid> <description><![CDATA[In this blog post I will list features, tips and tricks that one should know about when developing ASP.NET web applications (some of them will also work for ASP.NET MVC applications). The points listed below are not sorted in any way; it&#8217;s just a conglomeration of things that only need little (programming) effort to speed [...]
Related posts:<ol><li><a
href='http://blog.janjonas.net/2010-12-20/android-development-restart-application-programmatically' rel='bookmark' title='Android development: Restart application programmatically'>Android development: Restart application programmatically</a></li><li><a
href='http://blog.janjonas.net/2011-05-27/asp-net-mvc-how-to-add-elmah-error-logging-modules-handlers' rel='bookmark' title='ASP.NET MVC: How to add ELMAH (Error Logging Modules and Handlers for ASP.NET)'>ASP.NET MVC: How to add ELMAH (Error Logging Modules and Handlers for ASP.NET)</a></li><li><a
href='http://blog.janjonas.net/2011-04-03/asp_net-mvc_3-unity-of-work-pattern-unity_2' rel='bookmark' title='ASP.NET MVC 3: How to use the Unit of Work pattern with Unity 2.0'>ASP.NET MVC 3: How to use the Unit of Work pattern with Unity 2.0</a></li></ol>]]></description> <content:encoded><![CDATA[<p>In this blog post I will list features, tips and tricks that one should know about when developing ASP.NET web applications (some of them will also work for ASP.NET MVC applications). The points listed below are not sorted in any way; it&#8217;s just a conglomeration of things that only need little (programming) effort to speed up or support the development process. If you know other points that should be mentioned here, please leave me a comment.</p><h3>Tip 1: SMTP setting deliveryMethod &#8220;SpecifiedPickupDirectory&#8221;</h3><p>While developing an ASP.NET web application one could use the SMTP settings <em>deliveryMethod=&#8221;SpecifiedPickupDirectory&#8221;</em> to save all e-mails to a local directory (as <em>.eml</em> files) instead of sending them to a SMTP server.</p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  [...]
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;system.net<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;mailSettings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;smtp</span> <span style="color: #000066;">deliveryMethod</span>=<span style="color: #ff0000;">&quot;SpecifiedPickupDirectory&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;specifiedPickupDirectory</span> <span style="color: #000066;">pickupDirectoryLocation</span>=<span style="color: #ff0000;">&quot;c:\temp\&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/smtp<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/mailSettings<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/system.net<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  [...]
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p>Using this configuration in the development environment one neither need a running SMTP server nor one need to worry about sending any e-mail to (other) recipients. It could be reasonable to put the configuration in the <a
href="http://msdn.microsoft.com/en-us/library/ms229697%28v=vs.71%29.aspx" target="_blank"><em>machine.config</em> file</a> on the development machine to configure all applications to save the e-mails as files.</p><h3>Tip 2: Compilation setting &#8220;optimizeCompilations&#8221;</h3><p>To speed up the development process one could set the <em>optimizeCompilations</em> compilation setting to <em>true</em>. When setting this value to <em>true</em> only the affected files are recompiled when a change to a top-level file (for example a changed file in the <em>bin</em> folder) is detected. Especially when developing large web applications this could save much time because it significantly decreases the application&#8217;s start-up time.</p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  [...]
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;system.web<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;compilation</span> <span style="color: #000066;">optimizeCompilations</span>=<span style="color: #ff0000;">&quot;true&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      [...]
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/compilation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/system.web<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  [...]
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p>Depending on the type of changes one made to a top-level file, using this optimization could result in run-time errors (see <a
href="http://msdn.microsoft.com/en-us/library/ms366723.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/ms366723.aspx</a> for detailed information). To force a complete recompilation of the application just set the <em>optimizeCompilations</em> flag temporarily back to <em>false</em>.</p><h3>Tip 3: IIS: app_offline.htm offline page</h3><p>An easy way to (temporarily) put an ASP.NET web application offline is to create a file named <em>app_offline.htm</em> in the root directory of the application. The IIS web-server automatically shuts down the ASP.NET application and returns the content of the <em>app_offline.htm</em> (and a HTTP status code 503 &#8220;Service Unavailable&#8221;) to all new incoming requests. To support Internet Explorer 6, the <em>app_offline.htm</em> file needs at least 512 bytes of content (one can increase the file size for example by adding some html comments).</p><p>To start the ASP.NET application again just remove or rename the <em>app_offline.htm</em> file.</p><p>Note: Before .NET 4 the web-server returns a status code 404 (&#8220;Page not Found&#8221;) instead of 503 (&#8220;Service Unavailable&#8221;) when serving the <em>app_offline.htm</em> which could cause the web page from being removed from search engine indexes (see <a
href="http://connect.microsoft.com/VisualStudio/feedback/details/319986/app-offline-htm-file-should-cause-net-framework-to-return-http-status-code-of-503" target="_blank">here</a>). Therefore, when using an older version of the .NET framework it is recommendable to add a custom <em>aspx</em> page that is used as &#8220;offline page&#8221; and adding the following lines of code to set the correct HTTP status code:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;">Response<span style="color: #008000;">.</span><span style="color: #0000FF;">Status</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;503 Service Unavailable&quot;</span><span style="color: #008000;">;</span>
Response<span style="color: #008000;">.</span><span style="color: #0000FF;">StatusCode</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">503</span><span style="color: #008000;">;</span></pre></div></div><h3>Tip 4: Get Page instance from HttpContext object</h3><p>Use the following code snippet to get the Page instance from the (current) <em>HttpContext</em> object:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;">Page page <span style="color: #008000;">=</span> HttpContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Handler</span> <span style="color: #0600FF; font-weight: bold;">as</span> Page<span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>page <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// Do something with the Page instance</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #0600FF; font-weight: bold;">else</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// No Page instance available </span>
<span style="color: #008000;">&#125;</span></pre></div></div><h3>Tip 5: HttpContext.Items as request-level cache</h3><p>An easy way to cache arbitrary data in the scope of the current HTTP context is to use the <em>HttpContext.Items</em> dictionary:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;">HttpContext<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Items</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;MyKey&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> myObject<span style="color: #008000;">;</span></pre></div></div><p>A use case is shown in my blog post <a
href="http://blog.janjonas.net/2011-04-03/asp_net-mvc_3-unity-of-work-pattern-unity_2">&#8220;ASP.NET MVC 3: How to use the Unit of Work pattern with Unity 2.0&#8243;</a> when implementing the <em>HttpContextLifetimeManager</em>.</p><p>Related posts:<ol><li><a
href='http://blog.janjonas.net/2010-12-20/android-development-restart-application-programmatically' rel='bookmark' title='Android development: Restart application programmatically'>Android development: Restart application programmatically</a></li><li><a
href='http://blog.janjonas.net/2011-05-27/asp-net-mvc-how-to-add-elmah-error-logging-modules-handlers' rel='bookmark' title='ASP.NET MVC: How to add ELMAH (Error Logging Modules and Handlers for ASP.NET)'>ASP.NET MVC: How to add ELMAH (Error Logging Modules and Handlers for ASP.NET)</a></li><li><a
href='http://blog.janjonas.net/2011-04-03/asp_net-mvc_3-unity-of-work-pattern-unity_2' rel='bookmark' title='ASP.NET MVC 3: How to use the Unit of Work pattern with Unity 2.0'>ASP.NET MVC 3: How to use the Unit of Work pattern with Unity 2.0</a></li></ol></p><p><a
href="http://blog.janjonas.net/?flattrss_redirect&amp;id=1394&amp;md5=43007352c35b8509d948ae968c748387" title="Flattr" target="_blank"><img
src="http://blog.janjonas.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded> <wfw:commentRss>http://blog.janjonas.net/2011-12-28/asp_net-development-features-tips-tricks/feed</wfw:commentRss> <slash:comments>3</slash:comments> <atom:link rel="payment" href="http://blog.janjonas.net/?flattrss_redirect&amp;id=1394&amp;md5=43007352c35b8509d948ae968c748387" type="text/html" /> </item> <item><title>ASP.NET MVC 3: Set up custom error pages to handle errors in &#8220;non-AJAX&#8221; requests and jQuery AJAX requests</title><link>http://blog.janjonas.net/2011-12-11/asp-net-mvc3-custom-error-pages-non-ajax-requests-jquery-ajax-requests</link> <comments>http://blog.janjonas.net/2011-12-11/asp-net-mvc3-custom-error-pages-non-ajax-requests-jquery-ajax-requests#comments</comments> <pubDate>Sun, 11 Dec 2011 12:35:52 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Howto]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[.NET]]></category> <category><![CDATA[AJAX]]></category> <category><![CDATA[ASP.NET]]></category> <category><![CDATA[jQuery]]></category> <category><![CDATA[MVC]]></category> <category><![CDATA[MVC 3]]></category> <guid
isPermaLink="false">http://blog.janjonas.net/?p=1376</guid> <description><![CDATA[In this blog post I will show how to set up custom error pages in ASP.NET MVC 3 applications to show user-friendly error messages instead of the (yellow) IIS default error pages for both &#8220;normal&#8221; (non-AJAX) requests and jQuery AJAX requests. In this showcase we will implement custom error pages to handle the HTTP error [...]
Related posts:<ol><li><a
href='http://blog.janjonas.net/2011-04-13/asp_net-prevent-iis_75_overriding-custom-error-page-iis-default-error-page' rel='bookmark' title='ASP.NET: Prevent IIS 7.5 from overriding custom error pages with IIS default error pages'>ASP.NET: Prevent IIS 7.5 from overriding custom error pages with IIS default error pages</a></li><li><a
href='http://blog.janjonas.net/2011-08-07/asp_net-mvc_3-jquery-ajax-submit-ajax-form-supporting-unobtrusive-client-side-validation-and-server-side-validation' rel='bookmark' title='ASP.NET MVC 3: Using jQuery .ajax() function to submit Ajax Form supporting (unobtrusive) Client Side Validation and Server Side Validation'>ASP.NET MVC 3: Using jQuery .ajax() function to submit Ajax Form supporting (unobtrusive) Client Side Validation and Server Side Validation</a></li><li><a
href='http://blog.janjonas.net/2011-08-09/asp_net-mvc_3-json-result-jquery-ajax-form-validation' rel='bookmark' title='ASP.NET MVC 3: Using JSON result for jQuery Ajax Forms validation'>ASP.NET MVC 3: Using JSON result for jQuery Ajax Forms validation</a></li></ol>]]></description> <content:encoded><![CDATA[<p>In this blog post I will show how to set up custom error pages in ASP.NET MVC 3 applications to show user-friendly error messages instead of the (yellow) IIS default error pages for both &#8220;normal&#8221; (non-AJAX) requests and jQuery AJAX requests.</p><p>In this showcase we will implement custom error pages to handle the HTTP error codes 404 (&#8220;Not Found&#8221;) and 500 (&#8220;Internal server error&#8221;) which I think are the most common errors that could occur in web applications. In a first step we will set up the custom error pages to handle errors occurring in &#8220;normal&#8221; non-AJAX requests and in a second step we add a little JavaScript jQuery code that handles jQuery AJAX errors.</p><p>We start with a new (empty) ASP.NET MVC 3 project and activate custom errors in the <em>Web.config</em> by adding the following lines under <em>&lt;system.web&gt;</em>:</p><div
class="wp_syntax"><div
class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;customErrors</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">&quot;On&quot;</span> <span style="color: #000066;">defaultRedirect</span>=<span style="color: #ff0000;">&quot;/Error&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;error</span> <span style="color: #000066;">redirect</span>=<span style="color: #ff0000;">&quot;/Error/NotFound&quot;</span> <span style="color: #000066;">statusCode</span>=<span style="color: #ff0000;">&quot;404&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;error</span> <span style="color: #000066;">redirect</span>=<span style="color: #ff0000;">&quot;/Error/InternalServerError&quot;</span> <span style="color: #000066;">statusCode</span>=<span style="color: #ff0000;">&quot;500&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/customErrors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div><p>Note: You can set <em>mode=&#8221;Off&#8221;</em> to disable custom errors which could be helpful while developing or debugging. Setting <em>mode=&#8221;RemoteOnly&#8221;</em> activates custom errors only for remote clients, i.e. disables custom errors when accessing via <em>http://localhost/[...]</em>. In this example setting <em>mode=&#8221;On&#8221;</em> is fine since we want to test our custom errors. You can find more information about the <em>&lt;customErrors&gt;</em> element <a
href="http://msdn.microsoft.com/en-us/library/h0hfz6fc.aspx" target="_blank">here</a>.</p><p>In a next step we <strong>remove</strong> the following line in <em>Global.asax.cs</em> file:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;">filters<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> HandleErrorAttribute<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div><p>and add a new <em>ErrorController</em> (<em>Controllers/ErrorController.cs</em>):</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ErrorController <span style="color: #008000;">:</span> Controller
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> ActionResult Index<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> InternalServerError<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> ActionResult NotFound<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    Response<span style="color: #008000;">.</span><span style="color: #0000FF;">TrySkipIisCustomErrors</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
    Response<span style="color: #008000;">.</span><span style="color: #0000FF;">StatusCode</span> <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>HttpStatusCode<span style="color: #008000;">.</span><span style="color: #0000FF;">NotFound</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> View<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;NotFound&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> ActionResult InternalServerError<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    Response<span style="color: #008000;">.</span><span style="color: #0000FF;">TrySkipIisCustomErrors</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
    Response<span style="color: #008000;">.</span><span style="color: #0000FF;">StatusCode</span> <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>HttpStatusCode<span style="color: #008000;">.</span><span style="color: #0000FF;">InternalServerError</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> View<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;InternalServerError&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>In a last step we add the <em>ErrorController</em>&#8216;s views (<em>Views/Error/NotFound.cshtml</em> and <em>Views/Error/InternalServerError.cshtml</em>) that defines the (error) pages the end user will see in case of an error. The views include a partial view defined in <em>Views/Shared/Error/NotFoundInfo.cshtml</em> respectively <em>Views/Shared/Error/InternalServerErrorInfo.cshtml</em> that contains the concrete error messages. As we will see below using these partial views enables us to reuse the same error messages to handle AJAX errors.</p><p><em>Views/Error/NotFound.cshtml</em>:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;">@<span style="color: #008000;">&#123;</span>
  ViewBag<span style="color: #008000;">.</span><span style="color: #0000FF;">Title</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Not found&quot;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
@<span style="color: #008000;">&#123;</span>
  Html<span style="color: #008000;">.</span><span style="color: #0000FF;">RenderPartial</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Error/NotFoundInfo&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p><em>Views/Shared/Error/NotFoundInfo.cshtml</em>:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;">The URL you have requested was not found<span style="color: #008000;">.</span></pre></div></div><p><em>Views/Error/InternalServerError.cshtml</em>:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;">@<span style="color: #008000;">&#123;</span>
  ViewBag<span style="color: #008000;">.</span><span style="color: #0000FF;">Title</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Internal server error&quot;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
@<span style="color: #008000;">&#123;</span>
  Html<span style="color: #008000;">.</span><span style="color: #0000FF;">RenderPartial</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Error/InternalServerErrorInfo&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p><em>Views/Shared/Error/InternalServerErrorInfo.cshtml</em>:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;">An <span style="color: #0600FF; font-weight: bold;">internal</span> Server error occured<span style="color: #008000;">.</span></pre></div></div><p>To handle errors occurring in (jQuery) AJAX calls we will use <a
href="http://jqueryui.com/" target="_blank">jQuery UI</a> to show a dialog containing the error messages. In order to include jQuery UI we need to add two lines to <em>Views/Shared/_Layout.cshtml</em>:</p><div
class="wp_syntax"><div
class="code"><pre class="html" style="font-family:monospace;">&lt;link href=&quot;@Url.Content(&quot;~/Content/themes/base/jquery.ui.all.css&quot;)&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; /&gt;
&lt;script src=&quot;@Url.Content(&quot;~/Scripts/jquery-ui-1.8.11.min.js&quot;)&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;</pre></div></div><p>Moreover we add the following jQuery JavaScript code (defining the global AJAX error handling) and the Razor snippet (defining the dialog containers) to <em>Views/Shared/_Layout.cshtml</em>:</p><div
class="wp_syntax"><div
class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Initialize dialogs ...</span>
    <span style="color: #003366; font-weight: bold;">var</span> dialogOptions <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
      autoOpen<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
      draggable<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
      modal<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
      resizable<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
      title<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Error&quot;</span><span style="color: #339933;">,</span>
      closeOnEscape<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
      <span style="color: #000066;">open</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.ui-dialog-titlebar-close&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hide</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// Hide close button</span>
      buttons<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span>
        text<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Close&quot;</span><span style="color: #339933;">,</span>
        click<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;close&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
      <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#InternalServerErrorDialog&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span>dialogOptions<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#NotFoundInfoDialog&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span>dialogOptions<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Set up AJAX error handling ...</span>
    $<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ajaxError</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> jqXHR<span style="color: #339933;">,</span> ajaxSettings<span style="color: #339933;">,</span> thrownError<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>jqXHR.<span style="color: #000066;">status</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">404</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#NotFoundInfoDialog&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;open&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>jqXHR.<span style="color: #000066;">status</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">500</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#InternalServerErrorDialog&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">dialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;open&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Something unexpected happend :( ...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;</span>div id<span style="color: #008000;">=</span><span style="color: #666666;">&quot;NotFoundInfoDialog&quot;</span><span style="color: #008000;">&gt;</span>
  @<span style="color: #008000;">&#123;</span> Html<span style="color: #008000;">.</span><span style="color: #0000FF;">RenderPartial</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Error/NotFoundInfo&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&lt;/</span>div<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;</span>div id<span style="color: #008000;">=</span><span style="color: #666666;">&quot;InternalServerErrorDialog&quot;</span><span style="color: #008000;">&gt;</span>
  @<span style="color: #008000;">&#123;</span> Html<span style="color: #008000;">.</span><span style="color: #0000FF;">RenderPartial</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Error/InternalServerErrorInfo&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&lt;/</span>div<span style="color: #008000;">&gt;</span></pre></div></div><p>As you can see in the Razor snippet above we reuse the error texts defined in the partial views saved in <em>Views/Shared/Error/</em>.</p><p>To test our custom errors we define the <em>HomeController</em> (<em>Controllers/HomeController.cs</em>) as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;">  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> HomeController <span style="color: #008000;">:</span> Controller
  <span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> ActionResult Index<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> View<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> ActionResult Error500<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Exception<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>and the corresponding view <em>Views/Home/Index.cshtml</em>:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;">@<span style="color: #008000;">&#123;</span>
  ViewBag<span style="color: #008000;">.</span><span style="color: #0000FF;">Title</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;ViewPage1&quot;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">&lt;</span>script type<span style="color: #008000;">=</span><span style="color: #666666;">&quot;text/javascript&quot;</span><span style="color: #008000;">&gt;</span>
  $function <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    $<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;a.ajax&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">click</span><span style="color: #008000;">&#40;</span>function <span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">event</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
      <span style="color: #0600FF; font-weight: bold;">event</span><span style="color: #008000;">.</span><span style="color: #0000FF;">preventDefault</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
      $<span style="color: #008000;">.</span><span style="color: #0000FF;">ajax</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#123;</span>
      url<span style="color: #008000;">:</span> $<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">attr</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">'href'</span><span style="color: #008000;">&#41;</span>,
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&lt;/</span>script<span style="color: #008000;">&gt;</span>
&nbsp;
<span style="color: #008000;">&lt;</span>ul<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>li<span style="color: #008000;">&gt;</span>@Html<span style="color: #008000;">.</span><span style="color: #0000FF;">ActionLink</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Error 404 (Not Found)&quot;</span>, <span style="color: #666666;">&quot;Error404&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&lt;/</span>li<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>li<span style="color: #008000;">&gt;</span>@Html<span style="color: #008000;">.</span><span style="color: #0000FF;">ActionLink</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Error 404 (Not Found) [AJAX]&quot;</span>, <span style="color: #666666;">&quot;Error404&quot;</span>, <span style="color: #008000;">new</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span>, <span style="color: #008000;">new</span> <span style="color: #008000;">&#123;</span> <span style="color: #6666cc; font-weight: bold;">Class</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;ajax&quot;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&lt;/</span>li<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>li<span style="color: #008000;">&gt;</span>@Html<span style="color: #008000;">.</span><span style="color: #0000FF;">ActionLink</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Error 500 (Internal Server Error)&quot;</span>, <span style="color: #666666;">&quot;Error500&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&lt;/</span>li<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>li<span style="color: #008000;">&gt;</span>@Html<span style="color: #008000;">.</span><span style="color: #0000FF;">ActionLink</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Error 500 (Internal Server Error) [AJAX]&quot;</span>, <span style="color: #666666;">&quot;Error500&quot;</span>, <span style="color: #008000;">new</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">&#125;</span>, <span style="color: #008000;">new</span> <span style="color: #008000;">&#123;</span> <span style="color: #6666cc; font-weight: bold;">Class</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;ajax&quot;</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&lt;/</span>li<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>ul<span style="color: #008000;">&gt;</span></pre></div></div><p>To test the custom errors you can launch the project and click one of the four links defined in the view above. The &#8220;AJAX links&#8221; should open a dialog containing the error message and the &#8220;non-AJAX&#8221; links should redirect to a new page showing the same error message.</p><p>You can download the Visual Studio 2010 project containing all the source code <a
href="http://blog.janjonas.net/wp-content/uploads/2011/12/vs_2010-mvc-custom-error-pages-query-ajax.zip">here</a>.</p><p>Summarized this blog post shows how to set up custom errors that handle errors occurring in both AJAX requests and &#8220;non-AJAX&#8221; requests. Depending on the project, one could customize the example code shown above to handle other HTTP errors as well or to show more customized error messages or dialogs.</p><p>When thinking about error handling in ASP.NET MVC 3 applications you should take a look at <a
href="http://code.google.com/p/elmah/" target="_blank">ELMAH</a> (Error Logging Modules and Handlers for ASP.NET), a tool that logs/handles errors on server side and which works perfectly in combination with the approach shown above. I wrote <a
href="http://blog.janjonas.net/2011-05-27/asp-net-mvc-how-to-add-elmah-error-logging-modules-handlers">a blog post</a> about how to set up and configure ELMAH in a ASP.NET MVC application.</p><p>Related posts:<ol><li><a
href='http://blog.janjonas.net/2011-04-13/asp_net-prevent-iis_75_overriding-custom-error-page-iis-default-error-page' rel='bookmark' title='ASP.NET: Prevent IIS 7.5 from overriding custom error pages with IIS default error pages'>ASP.NET: Prevent IIS 7.5 from overriding custom error pages with IIS default error pages</a></li><li><a
href='http://blog.janjonas.net/2011-08-07/asp_net-mvc_3-jquery-ajax-submit-ajax-form-supporting-unobtrusive-client-side-validation-and-server-side-validation' rel='bookmark' title='ASP.NET MVC 3: Using jQuery .ajax() function to submit Ajax Form supporting (unobtrusive) Client Side Validation and Server Side Validation'>ASP.NET MVC 3: Using jQuery .ajax() function to submit Ajax Form supporting (unobtrusive) Client Side Validation and Server Side Validation</a></li><li><a
href='http://blog.janjonas.net/2011-08-09/asp_net-mvc_3-json-result-jquery-ajax-form-validation' rel='bookmark' title='ASP.NET MVC 3: Using JSON result for jQuery Ajax Forms validation'>ASP.NET MVC 3: Using JSON result for jQuery Ajax Forms validation</a></li></ol></p><p><a
href="http://blog.janjonas.net/?flattrss_redirect&amp;id=1376&amp;md5=572bcad0178ae857dbb3cf38b76e8d7b" title="Flattr" target="_blank"><img
src="http://blog.janjonas.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded> <wfw:commentRss>http://blog.janjonas.net/2011-12-11/asp-net-mvc3-custom-error-pages-non-ajax-requests-jquery-ajax-requests/feed</wfw:commentRss> <slash:comments>1</slash:comments> <atom:link rel="payment" href="http://blog.janjonas.net/?flattrss_redirect&amp;id=1376&amp;md5=572bcad0178ae857dbb3cf38b76e8d7b" type="text/html" /> </item> <item><title>ASP.NET MVC 3: Use re-mix library to add controller actions with mixins</title><link>http://blog.janjonas.net/2011-11-26/asp_net-mvc_3-remix-library-add-controller-actions-mixins</link> <comments>http://blog.janjonas.net/2011-11-26/asp_net-mvc_3-remix-library-add-controller-actions-mixins#comments</comments> <pubDate>Sat, 26 Nov 2011 18:02:53 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Howto]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[.NET]]></category> <category><![CDATA[ASP.NET]]></category> <category><![CDATA[mixin]]></category> <category><![CDATA[MVC]]></category> <category><![CDATA[MVC 3]]></category> <category><![CDATA[re-mix]]></category> <guid
isPermaLink="false">http://blog.janjonas.net/?p=1360</guid> <description><![CDATA[Update 2012-01-15 I&#8217;ve written a follow-up post &#8220;ASP.NET MVC 3: Use re-mix library to add controller actions with mixins &#8211; Part 2&#8243; that shows how to integrate Fabian&#8217;s suggestions into the approach described in this post. Update 2012-01-10 Fabian Schmied (re-mix developer) published a blog post &#8220;re-mix: Encapsulate and share ASP.NET MVC controller actions&#8221; which [...]
Related posts:<ol><li><a
href='http://blog.janjonas.net/2012-01-15/asp_net-mvc_3-remix-library-add-controller-actions-mixins-part2' rel='bookmark' title='ASP.NET MVC 3: Use re-mix library to add controller actions with mixins &#8211; Part 2'>ASP.NET MVC 3: Use re-mix library to add controller actions with mixins &#8211; Part 2</a></li><li><a
href='http://blog.janjonas.net/2011-06-18/aspnet-mvc3-controller-extension-method-render-partial-view-string' rel='bookmark' title='ASP.NET MVC3: Controller extension methods to render a partial view to string'>ASP.NET MVC3: Controller extension methods to render a partial view to string</a></li><li><a
href='http://blog.janjonas.net/2011-02-15/aspnet-mvc_3-get-controller-action-razor-view' rel='bookmark' title='ASP.NET MVC 3: How to get the current controller and action in a Razor view'>ASP.NET MVC 3: How to get the current controller and action in a Razor view</a></li></ol>]]></description> <content:encoded><![CDATA[<p><strong>Update 2012-01-15</strong> I&#8217;ve written a follow-up post <a
href="http://blog.janjonas.net/2012-01-15/asp_net-mvc_3-remix-library-add-controller-actions-mixins-part2">&#8220;ASP.NET MVC 3: Use re-mix library to add controller actions with mixins &#8211; Part 2&#8243;</a> that shows how to integrate <a
href="https://www.re-motion.org/blogs/mix/2012/01/10/re-mix-encapsulate-and-share-asp-net-mvc-controller-actions/" target="_blank">Fabian&#8217;s suggestions</a> into the approach described in this post.</p><p><strong>Update 2012-01-10</strong> Fabian Schmied (re-mix developer) published a blog post <a
href="https://www.re-motion.org/blogs/mix/2012/01/10/re-mix-encapsulate-and-share-asp-net-mvc-controller-actions/" target="_blank">&#8220;re-mix: Encapsulate and share ASP.NET MVC controller actions&#8221;</a> which shows possible solutions to the problems I&#8217;ve listed in this post.</p><p>When developing ASP.NET MVC3 application one often needs to add similar actions to different controllers; for example controllers that enable the user to perform the basic <a
href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete#User_interface" target="_blank">CRUD (create, read, update, delete) operations</a> typically contain action methods like <em>Create()</em>, <em>Update()</em> or <em>Delete()</em>. To avoid duplicate code a simple solution could be a common base controller. In complex applications this approach could have some limitations, since .NET does not support <a
href="http://en.wikipedia.org/wiki/Multiple_inheritance" target="_blank">multi inheritance</a>, e.g. each controller could have only one base controller.</p><p>This blog post shows a prototypically approach how to implement action methods that can be used in more than one controller with the help of the <a
href="http://remix.codeplex.com/" target="_blank">re-mix</a> library which adds <a
href="http://en.wikipedia.org/wiki/Mixins" target="_blank">mixins</a> support to .NET. Since the approach shown below is only a proof of concept, it has some limitations that are listed at the end of the post.</p><p>After <a
href="http://remix.codeplex.com/releases/" target="_blank">downloading re-mix</a> and adding references to the <em>dll</em> files contained in the folder <em>net-3.5\bin\release</em> one need a custom controller factory that uses remix&#8217;s <em>ObjectFactory</em> to create the controller objects:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> RemixControllerFactory <span style="color: #008000;">:</span> IControllerFactory
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> IControllerFactory _defaultControllerFactory<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> RemixControllerFactory<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    _defaultControllerFactory <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DefaultControllerFactory<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> IController CreateController<span style="color: #008000;">&#40;</span>RequestContext requestContext, <span style="color: #6666cc; font-weight: bold;">string</span> controllerName<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Use the default ControllerFactory to get the type of the controller</span>
    var controllerType <span style="color: #008000;">=</span> _defaultControllerFactory<span style="color: #008000;">.</span><span style="color: #0000FF;">CreateController</span><span style="color: #008000;">&#40;</span>requestContext, controllerName<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Use remix's ObjectFactory to instantiate the (mixed) controller</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>IController<span style="color: #008000;">&#41;</span>ObjectFactory<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span>controllerType<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> SessionStateBehavior GetControllerSessionBehavior<span style="color: #008000;">&#40;</span>RequestContext requestContext, <span style="color: #6666cc; font-weight: bold;">string</span> controllerName<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> SessionStateBehavior<span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Default</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> ReleaseController<span style="color: #008000;">&#40;</span>IController controller<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>The custom <em>RemixControllerFactory</em> is set as the default controller factory in the <em>Global.asax.as</em> file:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> Application_Start<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">&#91;</span><span style="color: #008000;">...</span><span style="color: #008000;">&#93;</span>
  ControllerBuilder<span style="color: #008000;">.</span><span style="color: #0000FF;">Current</span><span style="color: #008000;">.</span><span style="color: #0000FF;">SetControllerFactory</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> RemixControllerFactory<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#91;</span><span style="color: #008000;">...</span><span style="color: #008000;">&#93;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>In a next step we implement the interface that defines the action methods (<em>Echo</em> in this example) that will be used to extend the controllers:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> IEchoControllerMixin
<span style="color: #008000;">&#123;</span>
  ActionResult Echo<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> id<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>In a last step we implement the <em>IEchoControllerMixin</em>:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> EchoControllerMixin <span style="color: #008000;">:</span> Mixin<span style="color: #008000;">&lt;</span>ControllerBase<span style="color: #008000;">&gt;</span>, IEchoControllerMixin
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> ActionResult Echo<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> id<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    var result <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ContentResult<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    var controllerName <span style="color: #008000;">=</span> Target<span style="color: #008000;">.</span><span style="color: #0000FF;">ControllerContext</span><span style="color: #008000;">.</span><span style="color: #0000FF;">RouteData</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Values</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;controller&quot;</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    result<span style="color: #008000;">.</span><span style="color: #0000FF;">Content</span> <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;re-mix added Echo action to controller '{0}'&lt;hr/&gt;You said: {1}&quot;</span>, controllerName, <span style="color: #008000;">!</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">IsNullOrEmpty</span><span style="color: #008000;">&#40;</span>id<span style="color: #008000;">&#41;</span> <span style="color: #008000;">?</span> id <span style="color: #008000;">:</span> <span style="color: #666666;">&quot;&lt;i&gt;nothing&lt;/i&gt;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> result<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #008000;">&#91;</span>OverrideTarget<span style="color: #008000;">&#93;</span>
  <span style="color: #0600FF; font-weight: bold;">protected</span> IActionInvoker CreateActionInvoker<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> RemixControllerActionInvoker<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>IEchoControllerMixin<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>Due to security reasons ASP.NET MVC only allows to call action methods that are defined in a class that inherits <em>System.Web.Mvc.ControllerBase</em>. Since re-mix creates (automatically generated) proxy objects with their own inheritance structures, ASP.NET MVC does not find the <em>Echo</em> action by default. To bypass this limitation we implement a custom <em>RemixControllerActionInvoker</em> that allows calling actions that are defined in implementations of the <em>IEchoControllerMixin</em> interface. To do this we use reflection to call a non-public ASP.NET MVC functionality that creates an <em>ActionDescriptor</em> that skip the security check:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> RemixControllerActionInvoker <span style="color: #008000;">:</span> ControllerActionInvoker
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">private</span> Type _remixType<span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">public</span> RemixControllerActionInvoker<span style="color: #008000;">&#40;</span>Type remixType<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    _remixType <span style="color: #008000;">=</span> remixType<span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  <span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #0600FF; font-weight: bold;">override</span> ActionDescriptor FindAction<span style="color: #008000;">&#40;</span>ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, <span style="color: #6666cc; font-weight: bold;">string</span> actionName<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Use default action descriptor</span>
    ActionDescriptor actionDescriptor <span style="color: #008000;">=</span> controllerDescriptor<span style="color: #008000;">.</span><span style="color: #0000FF;">FindAction</span><span style="color: #008000;">&#40;</span>controllerContext, actionName<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>actionDescriptor <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #0600FF; font-weight: bold;">return</span> actionDescriptor<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Create action descriptor that accepts actions that are defined in the remix type</span>
    var internalConstructor <span style="color: #008000;">=</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>ReflectedActionDescriptor<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">GetConstructor</span><span style="color: #008000;">&#40;</span>
      BindingFlags<span style="color: #008000;">.</span><span style="color: #0000FF;">NonPublic</span> <span style="color: #008000;">|</span> BindingFlags<span style="color: #008000;">.</span><span style="color: #0000FF;">Instance</span>, <span style="color: #0600FF; font-weight: bold;">null</span>, <span style="color: #008000;">new</span> <span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>MethodInfo<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>ControllerDescriptor<span style="color: #008000;">&#41;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">bool</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#125;</span>, <span style="color: #0600FF; font-weight: bold;">null</span>
    <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">&#40;</span>ActionDescriptor<span style="color: #008000;">&#41;</span>internalConstructor<span style="color: #008000;">.</span><span style="color: #0000FF;">Invoke</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">object</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> _remixType<span style="color: #008000;">.</span><span style="color: #0000FF;">GetMethod</span><span style="color: #008000;">&#40;</span>actionName<span style="color: #008000;">&#41;</span>, actionName, controllerDescriptor, <span style="color: #0600FF; font-weight: bold;">false</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>After all the preparations we are finally able to &#8220;intermix&#8221; the <em>Echo</em> action to any controller:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Uses<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>EchoControllerMixin<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Controller1Controller <span style="color: #008000;">:</span> Controller
<span style="color: #008000;">&#123;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#91;</span>Uses<span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>EchoControllerMixin<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Controller2Controller <span style="color: #008000;">:</span> Controller
<span style="color: #008000;">&#123;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>To test the <em>Echo</em> action in both controllers, just open http://localhost:[...]/Controller1/Echo/Hello or http://localhost:[...]/Controller2/Echo/Hello.</p><p>Summarized mixins enable adding actions to controllers without the need of a common base controller; actions defined in mixins are completely decoupled from any inheritance structure and can be added to any controller.</p><p>As written in introduction, the approach shown above is only prototypical and has some limitations (which should be resolvable with some additional logic/code):</p><ol><li>Since the mixin overrides the <em>CreateActionInvoker()</em> of the controller that is &#8220;intermixed&#8221;, one could neither use more than one mixin in one controller nor use a custom <em>CreateActionInvoker()</em> method.</li><li>In actions defined in mixins one does not have access to the protected ASP.NET MVC controller methods that create <em>ActionResult</em> objects like <em>View()</em>, <em>PartialView()</em>, <em>Json()</em>, <em>Content()</em>, &#8230;</li><li>One needs to use reflection to call a protected <em>ReflectedActionDescriptor</em> constructor.</li><li>The <em>RemixControllerActionInvoker</em> implementation does not consider the action parameter when creating the action descriptor. In result the action names defined in the mixins needs to be unique.</li></ol><p>You can download the Visual Studio 2010 project containing all the source code <a
href="http://blog.janjonas.net/wp-content/uploads/2011/11/vs_2010-mvc-remix.zip">here</a>.</p><p>Related posts:<ol><li><a
href='http://blog.janjonas.net/2012-01-15/asp_net-mvc_3-remix-library-add-controller-actions-mixins-part2' rel='bookmark' title='ASP.NET MVC 3: Use re-mix library to add controller actions with mixins &#8211; Part 2'>ASP.NET MVC 3: Use re-mix library to add controller actions with mixins &#8211; Part 2</a></li><li><a
href='http://blog.janjonas.net/2011-06-18/aspnet-mvc3-controller-extension-method-render-partial-view-string' rel='bookmark' title='ASP.NET MVC3: Controller extension methods to render a partial view to string'>ASP.NET MVC3: Controller extension methods to render a partial view to string</a></li><li><a
href='http://blog.janjonas.net/2011-02-15/aspnet-mvc_3-get-controller-action-razor-view' rel='bookmark' title='ASP.NET MVC 3: How to get the current controller and action in a Razor view'>ASP.NET MVC 3: How to get the current controller and action in a Razor view</a></li></ol></p><p><a
href="http://blog.janjonas.net/?flattrss_redirect&amp;id=1360&amp;md5=0c23dcd55fe2984cdda038e385bb0047" title="Flattr" target="_blank"><img
src="http://blog.janjonas.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded> <wfw:commentRss>http://blog.janjonas.net/2011-11-26/asp_net-mvc_3-remix-library-add-controller-actions-mixins/feed</wfw:commentRss> <slash:comments>0</slash:comments> <atom:link rel="payment" href="http://blog.janjonas.net/?flattrss_redirect&amp;id=1360&amp;md5=0c23dcd55fe2984cdda038e385bb0047" type="text/html" /> </item> <item><title>WCF Services over HTTPS: Programmatically accept self-signed or invalid SSL certificates</title><link>http://blog.janjonas.net/2011-11-25/wcf-services-https-programmatically-accept-self-signed-invalid-ssl-certificates</link> <comments>http://blog.janjonas.net/2011-11-25/wcf-services-https-programmatically-accept-self-signed-invalid-ssl-certificates#comments</comments> <pubDate>Fri, 25 Nov 2011 18:56:57 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Howto]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[.NET]]></category> <category><![CDATA[SSL]]></category> <category><![CDATA[WCF]]></category> <guid
isPermaLink="false">http://blog.janjonas.net/?p=1347</guid> <description><![CDATA[When using WCF Services over HTTPS with a self-signed or invalid SSL certificates WCF could throw get an exception of type SecurityNegotiationException that says: Could not establish trust relationship for the SSL/TLS secure channel with authority [...]. To instruct WCF to accept all certificates use the following code before consuming any services: ServicePointManager.ServerCertificateValidationCallback += &#40;sender, [...]
Related posts:<ol><li><a
href='http://blog.janjonas.net/2010-02-01/cacert-apache2-free-public-key-certificate' rel='bookmark' title='CACert.org &amp; Apache2: Create and use free public key certificates'>CACert.org &#038; Apache2: Create and use free public key certificates</a></li><li><a
href='http://blog.janjonas.net/2011-01-19/asp_net-add-script-tags-include-javascript-master-pages-head-tag' rel='bookmark' title='ASP.NET: Programmatically adding &lt;script&gt; tags to include JavaScript files in the master page&#8217;s &lt;head&gt; tag'>ASP.NET: Programmatically adding &lt;script&gt; tags to include JavaScript files in the master page&#8217;s &lt;head&gt; tag</a></li><li><a
href='http://blog.janjonas.net/2010-12-20/android-development-restart-application-programmatically' rel='bookmark' title='Android development: Restart application programmatically'>Android development: Restart application programmatically</a></li></ol>]]></description> <content:encoded><![CDATA[<p>When using WCF Services over HTTPS with a self-signed or invalid SSL certificates WCF could throw get an exception of type <a
href="http://msdn.microsoft.com/en-us/library/system.servicemodel.security.securitynegotiationexception.aspx" target="_blank">SecurityNegotiationException</a> that says:</p><blockquote><p> Could not establish trust relationship for the SSL/TLS secure channel with authority [...].</p></blockquote><p>To instruct WCF to accept all certificates use the following code before consuming any services:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;">ServicePointManager<span style="color: #008000;">.</span><span style="color: #0000FF;">ServerCertificateValidationCallback</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">&#40;</span>sender, cert, chain, errors<span style="color: #008000;">&#41;</span> <span style="color: #008000;">=&gt;</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span></pre></div></div><p>Self-signed certificates are often used in development environments. Due to security reasons, in a production environment you should never use the code above; instead you should always use valid certificates and prohibit any communication in case of certificate problems!</p><p>Related posts:<ol><li><a
href='http://blog.janjonas.net/2010-02-01/cacert-apache2-free-public-key-certificate' rel='bookmark' title='CACert.org &amp; Apache2: Create and use free public key certificates'>CACert.org &#038; Apache2: Create and use free public key certificates</a></li><li><a
href='http://blog.janjonas.net/2011-01-19/asp_net-add-script-tags-include-javascript-master-pages-head-tag' rel='bookmark' title='ASP.NET: Programmatically adding &lt;script&gt; tags to include JavaScript files in the master page&#8217;s &lt;head&gt; tag'>ASP.NET: Programmatically adding &lt;script&gt; tags to include JavaScript files in the master page&#8217;s &lt;head&gt; tag</a></li><li><a
href='http://blog.janjonas.net/2010-12-20/android-development-restart-application-programmatically' rel='bookmark' title='Android development: Restart application programmatically'>Android development: Restart application programmatically</a></li></ol></p><p><a
href="http://blog.janjonas.net/?flattrss_redirect&amp;id=1347&amp;md5=0af0b8782c95ec2b1b69f271fdca22b5" title="Flattr" target="_blank"><img
src="http://blog.janjonas.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded> <wfw:commentRss>http://blog.janjonas.net/2011-11-25/wcf-services-https-programmatically-accept-self-signed-invalid-ssl-certificates/feed</wfw:commentRss> <slash:comments>0</slash:comments> <atom:link rel="payment" href="http://blog.janjonas.net/?flattrss_redirect&amp;id=1347&amp;md5=0af0b8782c95ec2b1b69f271fdca22b5" type="text/html" /> </item> <item><title>ASP.NET MVC 3: Strongly typed Authorize Attribute with multiple users and roles</title><link>http://blog.janjonas.net/2011-10-23/asp_net-mvc_3-strongly-typed-authorize-attribute-multiple-users-roles</link> <comments>http://blog.janjonas.net/2011-10-23/asp_net-mvc_3-strongly-typed-authorize-attribute-multiple-users-roles#comments</comments> <pubDate>Sun, 23 Oct 2011 16:23:42 +0000</pubDate> <dc:creator>admin</dc:creator> <category><![CDATA[Howto]]></category> <category><![CDATA[Programming]]></category> <category><![CDATA[ASP.NET]]></category> <category><![CDATA[MVC]]></category> <category><![CDATA[MVC 3]]></category> <guid
isPermaLink="false">http://blog.janjonas.net/?p=1335</guid> <description><![CDATA[The ASP.NET MVC 3 framework provides an Authorize attribute that enables you to easily restrict the access to specific controllers and/or actions to predefined roles or users. In detail, the controller or the action is marked with the Authorize attribute that defines which roles or users are granted access to the content which looks as [...]
Related posts:<ol><li><a
href='http://blog.janjonas.net/2012-01-04/asp_net-mvc_3-multiple-submit-buttons-one-form-html-helper-multisubmitbutton' rel='bookmark' title='ASP.NET MVC 3: Support multiple submit buttons in one form with HTML Helper MultiSubmitButton'>ASP.NET MVC 3: Support multiple submit buttons in one form with HTML Helper MultiSubmitButton</a></li><li><a
href='http://blog.janjonas.net/2012-01-15/asp_net-mvc_3-remix-library-add-controller-actions-mixins-part2' rel='bookmark' title='ASP.NET MVC 3: Use re-mix library to add controller actions with mixins &#8211; Part 2'>ASP.NET MVC 3: Use re-mix library to add controller actions with mixins &#8211; Part 2</a></li><li><a
href='http://blog.janjonas.net/2011-06-18/aspnet-mvc3-controller-extension-method-render-partial-view-string' rel='bookmark' title='ASP.NET MVC3: Controller extension methods to render a partial view to string'>ASP.NET MVC3: Controller extension methods to render a partial view to string</a></li></ol>]]></description> <content:encoded><![CDATA[<p>The ASP.NET MVC 3 framework provides an <a
href="http://msdn.microsoft.com/en-us/library/system.web.mvc.authorizeattribute.aspx" target="_blank"><em>Authorize</em> attribute</a> that enables you to easily restrict the access to specific controllers and/or actions to predefined roles or users. In detail, the controller or the action is marked with the <em>Authorize</em> attribute that defines which roles or users are granted access to the content which looks as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> ExampleController <span style="color: #008000;">:</span> Controller
<span style="color: #008000;">&#123;</span>
  <span style="color: #008000;">&#91;</span>Authorize<span style="color: #008000;">&#40;</span>Users<span style="color: #008000;">=</span><span style="color: #666666;">&quot;User1,User2&quot;</span>, Roles<span style="color: #008000;">=</span><span style="color: #666666;">&quot;Role1,Role2&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> ActionResult ExampleAction<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">&#91;</span><span style="color: #008000;">...</span><span style="color: #008000;">&#93;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>As you can see in the above example the users and roles are defined using a comma separated string. In complex applications it is often preferable to use a strongly typed approach instead of comma separated strings, which make renaming of roles error-prone and cumbersome. For example, the roles could be defined as constants in a utility class:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Roles
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> Role1 <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Role1&quot;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> Role2 <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Role2&quot;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> Role3 <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Role3&quot;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>Since one has to use a constant expression when defining attributes one cannot define the <em>Authorize</em> attribute as follows:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>Authorize<span style="color: #008000;">&#40;</span>Roles <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Join</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;,&quot;</span>, <span style="color: #008000;">new</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> Role1, Role2 <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span></pre></div></div><p>To overcome this limitation, I created a custom <em>MultiAuthorize</em> attribute that subclasses the default <em>Authorize</em> attribute and uses arrays to define the <em>Roles</em> and <em>Users</em> properties:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> MultiAuthorizeAttribute <span style="color: #008000;">:</span> AuthorizeAttribute
<span style="color: #008000;">&#123;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> Roles
  <span style="color: #008000;">&#123;</span>
    get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Roles</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Split</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">','</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    set <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Roles</span> <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Join</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;,&quot;</span>, value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> Users
  <span style="color: #008000;">&#123;</span>
    get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Users</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Split</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">','</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    set <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">base</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Users</span> <span style="color: #008000;">=</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Join</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;,&quot;</span>, value<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div><p>Using this custom attribute, one can use arrays containing the strongly-typed roles:</p><div
class="wp_syntax"><div
class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&#91;</span>MultiAuthorize<span style="color: #008000;">&#40;</span>Roles <span style="color: #008000;">=</span> <span style="color: #008000;">new</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> Role1, Role2 <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span></pre></div></div><p>Related posts:<ol><li><a
href='http://blog.janjonas.net/2012-01-04/asp_net-mvc_3-multiple-submit-buttons-one-form-html-helper-multisubmitbutton' rel='bookmark' title='ASP.NET MVC 3: Support multiple submit buttons in one form with HTML Helper MultiSubmitButton'>ASP.NET MVC 3: Support multiple submit buttons in one form with HTML Helper MultiSubmitButton</a></li><li><a
href='http://blog.janjonas.net/2012-01-15/asp_net-mvc_3-remix-library-add-controller-actions-mixins-part2' rel='bookmark' title='ASP.NET MVC 3: Use re-mix library to add controller actions with mixins &#8211; Part 2'>ASP.NET MVC 3: Use re-mix library to add controller actions with mixins &#8211; Part 2</a></li><li><a
href='http://blog.janjonas.net/2011-06-18/aspnet-mvc3-controller-extension-method-render-partial-view-string' rel='bookmark' title='ASP.NET MVC3: Controller extension methods to render a partial view to string'>ASP.NET MVC3: Controller extension methods to render a partial view to string</a></li></ol></p><p><a
href="http://blog.janjonas.net/?flattrss_redirect&amp;id=1335&amp;md5=95cfe14aefcc5518951b1154d220213e" title="Flattr" target="_blank"><img
src="http://blog.janjonas.net/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded> <wfw:commentRss>http://blog.janjonas.net/2011-10-23/asp_net-mvc_3-strongly-typed-authorize-attribute-multiple-users-roles/feed</wfw:commentRss> <slash:comments>0</slash:comments> <atom:link rel="payment" href="http://blog.janjonas.net/?flattrss_redirect&amp;id=1335&amp;md5=95cfe14aefcc5518951b1154d220213e" type="text/html" /> </item> </channel> </rss>
