Today I was trying to embed YAF (Yet Another Forum) in my MVC project. Everything was working fine except the Captcha  image which YAF use to show on their Register Page.

The Captcha image was not showing on the Register Page. See below image:

The issue was quiet confusing as everything was working fine when I embedded the same project with ASP.Net Web forms website , but the Captcha image was not coming after embedding YAF in ASP.NET MVC project.


After right clicking on the Image (to get Image URL) I found that this image was using some resource 'Resource.ashx' as its URL to show image.
See below image:

After going through the YAF code (YAF is open source) I found that YAF team has developed their own custom HttpHandler with name 'YAF.YafResourceHandler' which handles all the request ending in 'Resource.ashx'.

Here was the main flaw in the complication. Since in MVC 'url routing' is used which was restricting this resource.ashx request to be processed by server.

So , to enable YAF.YafResourceHandler  to process the resource.ashx request (resource used by the captcha image) , we have to specify its URL as ignoreURL in MVC which can be dome as follows:


1.) Go to your MVC application Route.config file and add following line   

          routes.IgnoreRoute("Resource.ashx"); in RegisterRoutes Function.

2.) The function will show now like this.

      public static void RegisterRoutes(RouteCollection routes)
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id =   
               UrlParameter.Optional }

Ignoring this url by MVC framework will enable the 'Resource.ashx' request to be handled by the custom HttpHandler (YAF.YafResourceHandler) and hence the image will start to shown on the register page.

Hope this will help. Happy coding.

For more on programming on this blog click here


Post a Comment