Hi Folks! Ever wanted to add QR codes to your webpages, so readers could easily open them in their smartphones without typing so much?

Did you know the QR code was invented in Japan by the Automotive Industry? Well, although my grandparents were from there and me being from Brazil, my heart is in CANADA.

Well, in this post I will show you 2 ways of generating QR code PNG images from ASP.NET: by coding a controller or adding an action method that returns a FileResult (MVC) or relying on HTTP handlers, a feature developers can use since the ASP.NET classic or webforms.

Find the github repository here.

Is there any difference between the 2 methods?

The source-code that will generate the images don't differ too much: both will use a third-party package called QrCode.NET from gmamaladze, silverlancer and iMarti. The following screenshot shows which package should be downloaded from NuGet:

QrCode.NET from NuGet Package

Now, in terms of ASP.NET infrastructure, they do differ! After downloading the source-code from GitHub, open the solution and go to the HomeController.cs and find a method called QrCode: from string a parameter called url, it generates the QR code with a fixed module size of 10 with 2 quiet zone modules (pretty straight-forward).

But whenever that action method is called, the request will go through a series of steps inside the ASP.NET MVC pipeline: from identifying an appropriate route for the current request, find the correspondent controller, the execution of action filters the controller might have attached to it, access to session storage and so on. A brief overview of the life cycle of an ASP.NET MVC application in the picture below:

It seems a bit overheaded for QR coding, right? So let's take a look at the second method!

Using an HTTP handler to handle the task

Go to the other library in the solution called httphandler and find the QrCodeHandler.cs code. You will see the class implementing an interface called IHttpAsyncHandler, which is a complement of the original IHttpHandler, containing 3 methods: BeginProcessRequestEndProcessRequest and ProcessRequest. What's interesting and actually pretty simple is that the process can be called asynchronously just by adding an instance of a delegate Action<HttpContext> and calling the main method, ProcessRequest, from BeginProcessRequest. Then the EndProcessRequest method will synchonize with the main thread by waiting for the end of the execution.

At this point, the application doesn't have access to the session storage (it makes sense) because it won't be needed and it would stack up some additional processing time. If access to session is really a thing, all we would have to do is add the IRequiresSessionState interface to the declaration part of the class before trying to use the information.

In the RouteConfig.cs file, the specific route for the http handler needs to be processed apart from the MVC application lifecycle. That's why there's a line routes.IgnoreRoute("{resource}.qrcode/{*pathInfo}"); in it.

Now, run the web application. When a request for a resource with extension .qrcode is made from Home/Index, the request still goes through authentication and authorization steps, before being handed over to our QrCodeHandler class. If your application doesn't have HTTP modules for those requirements, the time required to process those steps would be saved. And we still have access to the cache storage, more appropriate if someone wants to save the generated qr codes in memory and avoid repetition.

QR Codes from several URLs

Inside the web.config file, go to a node called system.webServer/handlers. There you will see the configuration responsible for activating the handler when a request asks for a .qrcode resource extension. The IgnoreRoute line is still required for the sample to work properly though.

If you need to customize the code from the handler, but your classes are not thread-safe, you can remove the IHttpAsyncHandler interface and replace with the IHttpHandler.

 Leave a comment if you want to share anything. Enjoy!