How to use a custom HttpHandler in MVC?http://forums.asp.net/t/1320309.aspx/1?How+to+use+a+custom+HttpHandler+in+MVC+Fri, 12 Feb 2010 16:16:14 -050013203092622558http://forums.asp.net/p/1320309/2622558.aspx/1?How+to+use+a+custom+HttpHandler+in+MVC+How to use a custom HttpHandler in MVC? <p>Does anyone have an example of how to use a custom HttpHandler in MVC?</p> <p>I wrote a simple image handler that displays thumbnails on a view, but I can't get it to fire.</p> <p>I have it set in my web.config under the httpHandlers section, and in my html like this:</p> <p>&nbsp;</p> <pre class="prettyprint">&lt;img src=&quot;/image.ashx?PhotoID=1&amp;Size=M&quot; /&gt;</pre> <p>&nbsp; <br> </p> <p>The handler never fires.&nbsp; What am I missing?&nbsp; Do I need to create a route for it also?</p> <p>Thanks. <br> </p> 2008-09-15T16:56:52-04:002622726http://forums.asp.net/p/1320309/2622726.aspx/1?Re+How+to+use+a+custom+HttpHandler+in+MVC+Re: How to use a custom HttpHandler in MVC? <p>You can use the handler normal like other applications, no need to routes for using it.</p> <p>may be you have error on the code or wronge path or something else.</p> 2008-09-15T18:22:55-04:002622765http://forums.asp.net/p/1320309/2622765.aspx/1?Re+How+to+use+a+custom+HttpHandler+in+MVC+Re: How to use a custom HttpHandler in MVC? <p>&nbsp;If you run your application in IIS7 Integrated mode, the http handler must be defined in <b>handlers</b> element of <b>system.webServer</b> section (<b>httpHandlers</b> section works for IIS6 and IIS7 Classic mode).<br> </p> 2008-09-15T18:40:45-04:002623610http://forums.asp.net/p/1320309/2623610.aspx/1?Re+How+to+use+a+custom+HttpHandler+in+MVC+Re: How to use a custom HttpHandler in MVC? <p>Also check <a href="http://blog.maartenballiauw.be/post/2008/05/ASPNET-MVC-custom-ActionResult.aspx"> http://blog.maartenballiauw.be/post/2008/05/ASPNET-MVC-custom-ActionResult.aspx</a>, it features an image action result which just uses the standerd ASP.NET MVC lifecycle.</p> 2008-09-16T06:11:14-04:002625255http://forums.asp.net/p/1320309/2625255.aspx/1?Re+How+to+use+a+custom+HttpHandler+in+MVC+Re: How to use a custom HttpHandler in MVC? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>Ahmed Abu Dagga</h4> <p>You can use the handler normal like other applications, no need to routes for using it.</p> <p>may be you have error on the code or wronge path or something else.</p> <p></p> </blockquote> <p></p> <p>It doesn't seem like I have an error in my code because none is thrown.&nbsp; When I step through the code, it never gets to the handler.&nbsp; </p> 2008-09-16T18:36:29-04:002625264http://forums.asp.net/p/1320309/2625264.aspx/1?Re+How+to+use+a+custom+HttpHandler+in+MVC+Re: How to use a custom HttpHandler in MVC? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>CW2</h4> <p>&nbsp;If you run your application in IIS7 Integrated mode, the http handler must be defined in <b>handlers</b> element of <b>system.webServer</b> section (<b>httpHandlers</b> section works for IIS6 and IIS7 Classic mode).<br> </p> <p></p> </blockquote> <p></p> <p>It will be running on IIS 6 and I have all the necessary web.config settings made.</p> <p>When I debug and place a breakpoint in the handler, the pages loads without ever stepping into the handler.&nbsp; Any thoughts?</p> 2008-09-16T18:38:44-04:002625280http://forums.asp.net/p/1320309/2625280.aspx/1?Re+How+to+use+a+custom+HttpHandler+in+MVC+Re: How to use a custom HttpHandler in MVC? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>maartenba</h4> <p>Also check <a href="http://blog.maartenballiauw.be/post/2008/05/ASPNET-MVC-custom-ActionResult.aspx"> http://blog.maartenballiauw.be/post/2008/05/ASPNET-MVC-custom-ActionResult.aspx</a>, it features an image action result which just uses the standerd ASP.NET MVC lifecycle.</p> <p></p> </blockquote> <p></p> <p>Maarten, this does give me ideas.&nbsp; I'll give it a try.&nbsp; But I would still like to know why my handler never gets fired.&nbsp; [:S]</p> 2008-09-16T18:46:33-04:002625301http://forums.asp.net/p/1320309/2625301.aspx/1?Re+How+to+use+a+custom+HttpHandler+in+MVC+Re: How to use a custom HttpHandler in MVC? <ul> <li>&lt;div mce_keep=&quot;true&quot;&gt;Is your path in the image OK? (i.e. if you type <a href="http://localhost:whatever//image.ashx?PhotoID=1&amp;Size=M"> http://localhost:whatever//image.ashx?PhotoID=1&amp;Size=M</a>&nbsp;in your browser, is it fired?)&lt;/div&gt; </li><li>&lt;div mce_keep=&quot;true&quot;&gt;Try adding the following to global.asax.cs:<br> &nbsp;<pre class="prettyprint">routes.IgnoreRoute(&quot;{handler}.ashx?{*path}&quot;, new { handler = @&quot;image&quot; });</pre>&nbsp;&lt;/div&gt; </li><li>&lt;div mce_keep=&quot;true&quot;&gt;Just to be sure... Can you post your web.config (httpHandlers section) and global.asax.cs class?&lt;/div&gt;</li></ul> 2008-09-16T18:58:43-04:002625837http://forums.asp.net/p/1320309/2625837.aspx/1?Re+How+to+use+a+custom+HttpHandler+in+MVC+Re: How to use a custom HttpHandler in MVC? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>maartenba</h4> <p></p> <ul> <li>&lt;div&gt;Is your path in the image OK? (i.e. if you type <a href="http://localhost:whatever//image.ashx?PhotoID=1&amp;Size=M"> http://localhost:whatever//image.ashx?PhotoID=1&amp;Size=M</a>&nbsp;in your browser, is it fired?)&lt;/div&gt; </li><li>&lt;div&gt;Try adding the following to global.asax.cs:<br> &nbsp;<pre class="prettyprint">routes.IgnoreRoute(&quot;{handler}.ashx?{*path}&quot;, new { handler = @&quot;image&quot; });</pre>&nbsp;&lt;/div&gt;</li> <li> &lt;div&gt;Just to be sure... Can you post your web.config (httpHandlers section) and global.asax.cs class?&lt;/div&gt;</li></ul><p></blockquote>&nbsp;</p><p>Item 1: Image path - no I don't think the path is ok.&nbsp; If I enter the complete path to the handler I get a controller error. (I'm using Dependency Injection, see below) Here's the html for the image tag:</p><p>&nbsp;</p><pre class="prettyprint">&lt;<span class="tag">img</span><span class="attr"> src=</span><span class="attrv">"ImageHandler.ashx?PhotoID=1&amp;Size=M"</span><span class="attr"> alt=</span><span class="attrv">"Photo Number 1"</span> /&gt;</pre>&nbsp;&nbsp;<p>Here's my web.config httpHandlers section:</p><p>&nbsp;</p><pre class="prettyprint">&lt;<span class="tag">httpHandlers</span>&gt;<br> &lt;<span class="tag">remove</span><span class="attr"> verb=</span><span class="attrv">"*"</span><span class="attr"> path=</span><span class="attrv">"*.asmx"</span>/&gt;<br> &lt;<span class="tag">add</span><span class="attr"> verb=</span><span class="attrv">"*"</span><span class="attr"> path=</span><span class="attrv">"*.asmx"</span><span class="attr"> validate=</span><span class="attrv">"false"</span><span class="attr"> type=</span><span class="attrv">"System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"</span>/&gt;<br> &lt;<span class="tag">add</span><span class="attr"> verb=</span><span class="attrv">"*"</span><span class="attr"> path=</span><span class="attrv">"*_AppService.axd"</span><span class="attr"> validate=</span><span class="attrv">"false"</span><span class="attr"> type=</span><span class="attrv">"System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"</span>/&gt;<br> &lt;<span class="tag">add</span><span class="attr"> verb=</span><span class="attrv">"GET,HEAD"</span><span class="attr"> path=</span><span class="attrv">"ScriptResource.axd"</span><span class="attr"> type=</span><span class="attrv">"System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"</span><span class="attr"> validate=</span><span class="attrv">"false"</span>/&gt;<br> &lt;<span class="tag">add</span><span class="attr"> verb=</span><span class="attrv">"*"</span><span class="attr"> path=</span><span class="attrv">"*.mvc"</span><span class="attr"> validate=</span><span class="attrv">"false"</span><span class="attr"> type=</span><span class="attrv">"System.Web.Mvc.MvcHttpHandler, System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"</span>/&gt;<br> &lt;<span class="tag">add</span><span class="attr"> verb=</span><span class="attrv">"*"</span><span class="attr"> path=</span><span class="attrv">"ImageHandler.ashx"</span><span class="attr"> validate=</span><span class="attrv">"false"</span><span class="attr"> type=</span><span class="attrv">"PersonalKit.Mvc.Web.Views.Shared.ImageHandler, PersonalKit.Mvc.Core"</span>/&gt;<br>&lt;/<span class="tag">httpHandlers</span>&gt;<br></pre>&nbsp;&nbsp;<p>Just so you know, and this could be an issue, the location of the physical ashx file is: /Views/Shared/Imagehandler.ashx.</p><p>I did try adding the path to the img tag, but that didn't work either.&nbsp; No matter what I do, the handler does not get fired.</p><p>What does the routes.IgnoreRoute(...) do?&nbsp; It seems like it should ignore anything with this filename or extension.&nbsp; Is that not the case?</p><p>Here's something else that might help you, I'm using a Dependency Injector to handle the mapping to classes.&nbsp; Do you think I need to map this some how?&nbsp; I'm using the Microsoft.Practices.Unity container for the IoC.&nbsp; I'm not sure if this is part of the problem, but it might be.</p><p>Thanks for your help.</p><p>Here's my Global.asax class:</p><p>&nbsp;</p><pre class="prettyprint"><span class="kwd">using</span> System;<br><span class="kwd">using</span> System.Collections.Generic;<br><span class="kwd">using</span> System.Linq;<br><span class="kwd">using</span> System.Web;<br><span class="kwd">using</span> System.Web.Mvc;<br><span class="kwd">using</span> System.Web.Routing;<br><span class="kwd">using</span> System.Configuration;<br><br><span class="kwd">using</span> Microsoft.Practices.Unity;<br><span class="kwd">using</span> Microsoft.Practices.Unity.Configuration;<br><br><span class="kwd">using</span> PersonalKit.Mvc.Services;<br><span class="kwd">using</span> PersonalKit.Mvc.Data;<br><span class="cmt">//using PersonalKit.Mvc.Core.HttpHandlers;</span> <span class="kwd">using</span> PersonalKit.Mvc.Web.Views.Shared;<br><br><span class="kwd">namespace</span> PersonalKit.Mvc.Web<br>{<br> <span class="kwd">public class</span> MvcApplication : System.Web.HttpApplication<br> {<br> <span class="kwd">public static void</span> RegisterRoutes(RouteCollection routes)<br> {<br> routes.IgnoreRoute(<span class="st">"{resource}.axd/{*pathInfo}"</span>);<br><br> routes.MapRoute(<br> <span class="st">"Default"</span>, <span class="cmt">// Route name</span> <span class="st">"{controller}/{action}/{id}"</span>, <span class="cmt">// URL with parameters</span> <span class="kwd">new</span> { controller = <span class="st">"Home"</span>, action = <span class="st">"Index"</span>, id = <span class="st">""</span> } <span class="cmt">// Parameter defaults</span> ); } <span class="kwd">protected void</span> Application_Start()<br> {<br> IUnityContainer container = <span class="kwd">new</span> UnityContainer();<br><br> UnityConfigurationSection section = (UnityConfigurationSection)ConfigurationManager.GetSection(<span class="st">"unity"</span>);<br> section.Containers.Default.Configure(container);<br><br> UnityController.UnityControllerFactory factory = <span class="kwd">new</span> PersonalKit.Mvc.UnityController.UnityControllerFactory(container);<br> ControllerBuilder.Current.SetControllerFactory(factory);<br><br> container.Resolve<ImageHandler>();<br><br> RegisterRoutes(RouteTable.Routes);<br> }<br> }<br>}</ImageHandler></pre>&nbsp;&nbsp; <p><br> &nbsp;</p> </li></ul> </blockquote> 2008-09-17T01:55:03-04:002626222http://forums.asp.net/p/1320309/2626222.aspx/1?Re+How+to+use+a+custom+HttpHandler+in+MVC+Re: How to use a custom HttpHandler in MVC? Can you try putting the handler in the root of the website? 2008-09-17T06:13:46-04:002630577http://forums.asp.net/p/1320309/2630577.aspx/1?Re+How+to+use+a+custom+HttpHandler+in+MVC+Re: How to use a custom HttpHandler in MVC? <p></p> <blockquote><span class="icon-blockquote"></span> <h4>maartenba</h4> Can you try putting the handler in the root of the website?</blockquote> <p></p> <p>&nbsp;</p> <p>That didn't work either.&nbsp; I think it might have something to do with the Dependency Injection I'm using.&nbsp; </p> <p>I'll keep looking.&nbsp; I'm determined to figure this out!&nbsp; [:)]</p> <p>Thanks.</p> 2008-09-18T16:35:52-04:003334397http://forums.asp.net/p/1320309/3334397.aspx/1?Re+How+to+use+a+custom+HttpHandler+in+MVC+Re: How to use a custom HttpHandler in MVC? <p>FYI:</p> <p>I had this same problem and just solved it by adding an IgnoreRoute:</p> <pre class="prettyprint">public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute(&quot;{resource}.axd/{*pathInfo}&quot;); routes.IgnoreRoute(&quot;Custom.sfx&quot;); }</pre> <p>Provided classic/integration httpHandlers/handlers is setup properly and no authentication is blocking you it should be good to go.</p> <p><br> </p> <p><br> </p> 2009-08-07T00:46:16-04:003676591http://forums.asp.net/p/1320309/3676591.aspx/1?Re+How+to+use+a+custom+HttpHandler+in+MVC+Re: How to use a custom HttpHandler in MVC? <p>I was up against this problem last night, and here is how I solved it</p> <p>I added this to my Global.asax file, before I defined my routes :</p> <p>routes.IgnoreRoute(&quot;{filename}.ashx&quot;);</p> <p><br> </p> <p>I added this to my &lt;httpHandlers&gt; node in my web config:</p> <p><b>&lt;add verb=&quot;*&quot; path=&quot;*ImgHandler.ashx&quot; type=&quot;AMSignsUI.Views.Shared.ImgHandler, AMSignsUI&quot;/&gt;</b></p> <p>where : AMSignsUI.Views.Shared = the namespace in which I created the handler </p> <p>and AMSignsUI = my application/project name</p> <p>I added this to my &lt;handlers&gt; node in &lt;system.webServer&gt; </p> <p><b>&lt;add name=&quot;ImgHandler&quot; path=&quot;*ImgHandler.ashx&quot; verb=&quot;*&quot; type=&quot;AMSignsUI.Views.Shared.ImgHandler, AMSignsUI&quot;/&gt;</b></p> <p><br> </p> <p>Everything worked fine after I completed these steps. Note, <b>the last step applies only if you are using IIS7</b><br> </p> 2010-02-12T16:16:14-05:00