Introduction
In this article, we are going to create an ASP.Net Core MVC application that generates QR Code for different purposes like Email, Website, Bookmark Websites, SMS, WhatsApp, etc. For this, we are going to use NuGet package called QRCoder.
About QRCoder NuGet Package
QRCoder is a simple library, written in C#.NET, which enables you to create QR codes. It hasn't any dependencies on other libraries and is available as .NET Framework and .NET Core PCL version on NuGet.
QRCoder provides many types of QR Codes but in this article, I will use only the following types.
- URL
- Bookmark Website
- Send SMS
- Send WhatsApp Message
- Compose Email
- Connect To WIFI
Create ASP.Net Core MVC project.
Step 1
Open Visual Studio and click on Create New Project.
Step 2
Select ASP.NET CORE Web App (Model-View-Controller) Template as shown in the below image and click on next button.
Step 3
Configure your project by specifying the name of the project and the location where you want to save your project.
Step 4
Select the version of the .Net Core framework you want to use and click on create button.
Add New Controller
Step 1
Right Click on Controller Folder then click on Add then Controller.
Step 2
Select MVC Empty Controller. And give a name whatever you want here I gave QRCode.
Add NuGet Package
Step 1
Right click on your project and then click on Manage NuGet Packages.
Step 2
Search for QRCoder in browse tab.
Step 3
Click on the install button and it will install in your project.
Create a new Model Class
For transferring data from controller to view and view to controller we have to use model class.
Step 1
For creating a model right click on the Model folder and click on Add then class.
Step 2
Give a suitable name for your class, Here I gave QRCodeModel.
Step 3
Create required properties in your class. As you can see in the below code I have created different properties for different types of QR Codes.
Design View
Step 1
Create an action method in your controller as you want. Here I used the index action method.
Step 2
Right-click on the action method and click on Add View to add a new view.
Step 3
Return the model from the controller which you have created. If you want to pass some extra data you can pass it. Here as you see in the below code I just created object of model and pass that model to view.
Step 4
Design your view as per your requirements.
Code Explanation
As you can see in the above HTML code I created inputs as per my properties.
Here I add drop-down for QR Code type and based on that type I show and hide DIV of codes.
For different types of QR Codes, I have created different div and gave an id to that div same as I gave the value in dropdown. Also gave same class name to all div.
At bottom, there are two buttons for submitting and resetting page.
Below button, there is an image that shows QR Code Image when the view return from controller after generating QR Code. Here we directly pass QR Code Image as base64 string instead of saving image and then show it on view side.
At last, there is JavaScript code in the Scripts section which contain a function called onQRCodeTypeChange which gets value of QR Code Type drop-down then hide all the divs and only show which has id same as drop down value.
On the document ready event I triggered the change event of QR Code type drop-down.
Code for Generate QR Code
Create post action method in your controller to post data from form. In this method, we are going to generate QR Code as per its type.
Step 1
Create a new object of QRCodeGenerator class.
Step 2
Call the CreateQrCode method using this object and pass payload in its constructor. Payload is an object of PayloadGenerator.Payload type. There are different payload types as per QR Code types. We will discuss different types of payload later in this article.
Step 3
Create object of QRCode type and Pass QR Data object to its constructor.
Step 4
Call GetGraphic method of this QR Code object which take one integer parameter which define pixels per module.
There are also some overloaded methods using which you can change the color of QR Code and also set logo in QR Code.
Step 5
GetGraphic method return bitmap image as return type. You can save this image in your local path but here I convert this bitmap image to byte array and then convert this byte array to base64 string which I’m going to bind in QRImageURL property and pass it to the view so user can view this QR Code.
For converting bitmap to byte first convert it to memory stream and then get byte array from this stream in user define method BitmapToByteArray. As you can see in below code.
Step 6
Return view data after generating QR Code so user can see Generated QR Code in their browser.
Followings are different payload type which is used in this article.
URL Payload
To generate QR Code that opens URL or any Website QRCoder provider payload type of URL class.
This class takes one argument in its constructor which is URL of website.
Bookmark Payload
To generate QR Code for bookmark type we have to Use Bookmark payload.
Bookmark class’s constructor takes two parameters first is the URL of the website and second is the title of the bookmark.
SMS Payload
To generate QR Code for composing SMS we have to pass payload of SMS type. QR Code provider SMS class for this type of payload.
SMS class’s constructor takes two parameters first is phone number to whom you want to send the SMS and the second is the message you want to send. Message parameter is optional you can only pass phone number to it.
WhatsApp Message Payload
To generate QR Code that send WhatsApp message we have to pass WhatsAppMessage payload. QRCoder provides WhatsAppMessage class to generate payload.
WhatsAppMessage provides two overloaded methods one only takes one parameter which is message only and second takes two parameter WhatsApp number and message both.
Mail Payload
To generate QR Code which compose email we have to use MAIL payload.
Mail class’s constructor does not required any parameter. It’s all parameters are by default set to null. But here we are going to pass receiver email address, Subject, and body of mail.
WiFi Payload
To generate QR Code which connect to WIFI we have to pass data in WIFI payload.
WiFi class’s constructor takes three required parameter wifi name, wifi password, and authentication mode (WEP, WPA, nopass). You can pass any theme but mostly WPA is used in all wifi so here I by default set it.
I hope you find this article helpful. You can download source code from my GitHub
0 Comments