• nbolt
  • [ 5.0 , 1 ]
login to rate this
.NET BlogEngine 4.0 [non-official]
0 0
  • nbolt
  • [ 5.0 , 1 ]
login to rate this
[.NET7] Design Patterns: Facade in a Web Application
5 0
  • nbolt
  • [ 5.0 , 1 ]
login to rate this
[.NET7] Design Patterns: Repository in a Web Application
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
[.NET7] IP GeoLocation with Dependency Injection and Custom Middleware
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
[.NET7] Weather Forecast with Dependency Injection and Custom Middleware
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
2 ways for creating QR Code using ASP.NET
0 0
  • nbolt
  • [ 5.0 , 1 ]
login to rate this
AMD Ryzen 9 home server with Fedora 41 and .NET 8
17 1
  • ☺️ 1
    ❤️ 2
    🤩 1
    🫠 1
    😁 3
    😅 2
    😆 2
    😉 1
    😊 1
    😗 1
    😘 1
    😚 1
    🙂 1
    🙃 1
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
ASP.NET MVC Routing with @
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
ASP.NET MVC Routing with Constraints
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
BlogEngine.NET 3.3.5 with a BBQ Sauce
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
BlogEngine.NET 3.3.5 with a BBQ Sauce: Part 2
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
EF7 on .NET Core 7 (Preview): Fedora 35 x Windows 11 (41 Million Rows)
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
Entity Framework Core 6: Fedora 35 x Windows 11 (41 Million Rows)
0 0
  • nbolt
  • [ 5.0 , 1 ]
login to rate this
Entity Framework Core 7 (Preview): Fedora 35 x Windows 11 (41 Million Rows)
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
Entity Framework Providers for the .NET BlogEngine
0 0
  • nbolt
  • [ 5.0 , 1 ]
login to rate this
Enum as String in EF Core
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
Facebook: Data Deletion Callback URL in C#
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
How to fix the Could not load file or assembly 'System.IO.Compression' or one of its dependencies exception
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
How to Recover Access to your Account in an ASP.NET Application using the Membership (without decrypting your password)
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
Installing and Configuring Jenkins with GIT to Build and Deploy Your .NET Application
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
Installing and Configuring Jenkins with SVN to Build and Deploy Your .NET Application
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
IP GeoLocation with Unity and Dependency Injection
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
NHibernate Providers for the .NET BlogEngine
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
No mercy for the ORMs: Load and Performance Test on EF Core, NHibernate and pure ADO.NET (41M rows)
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
Resizing images on a Controller Method in ASP.NET Core (.NET 6)
0 0
  • 🔥 1
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
Streaming Video Files in ASP.NET MVC
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
Testing your MVC App on XSP/mono in Fedora (before deploying to Apache)
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
Weather with Unity and Dependency Injection
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
2 ways for creating QR Code using ASP.NET
0 0

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!

what to see next
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
Resizing images on a Controller Method in ASP.NET Core (.NET 6)
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
Streaming Video Files in ASP.NET MVC
0 0
  • nbolt
  • [ 0.0 , 0 ]
login to rate this
Weather with Unity and Dependency Injection
0 0
comment section
place your comment here, but login first
be the first to comment
An error has occurred . This application may no longer respond until reloaded . An unhandled exception has occurred . See browser dev tools for details . Reload 🗙