Introduction
This article discusses dynamically generating QR codes in ASP.NET MVC. It covers generating QR codes, creating QR code images, and working with QR code-related tasks in ASP.NET MVC using a library called IronBarCode.
You can install this library via Visual Studio's NuGet package manager to enable dynamic QR code generation, QR code text creation, reading QR codes, and image creation in ASP.NET MVC.
I will walk you through the steps to generate a QR code image from the generated QR code text.
Cross-Platform Support
IronBarcode is designed for use with C#, F#, and VB.NET, and it can run on .NET 7, 6, 5, Core, Standard, or .NET Framework.
Creating a New Project in Visual Studio 2022
-
Launch Visual Studio and select Create a New Project.
![Create New Project]()
Create a Web App
-
In the Create a New Project dialog, select ASP.NET Web Application (.NET Framework).
-
Configure your new project by entering the project name as "IronQRCode_Demo," choosing a location, and setting the solution name. Select .NET Framework 4.8 and click Create.
![Configure your new Project]()
Create a New ASP.NET Web Application
Select the MVC (Model-View-Controller) template from the Create New ASP.NET Web Application dialog and click Create.
![Create a New ASP.NET Web Application]()
Visual Studio will create a new MVC web application for you
Find and Install NuGet Packages
Install the Barcode Library through NuGet Package Manager
-
Go to Project > Manage NuGet Packages.
-
On the NuGet Package Manager page, choose nuget.org as the Package source.
-
Under the Browse tab, search for IronBarcode, select BarCode from the list, and then click Install.
-
If prompted to verify the installation, select OK.
![Serach IronBarCode NuGet Package]()
Using the Visual Studio Command-Line
-
In Visual Studio, go to Tools > NuGet Package Manager > Package Manager Console.
-
In the package manager console tab, enter the following command:
This will download and install the package into your current project, making it ready for use.
![Package manager console]()
Add a data model class
Right-click the Models folder > Add > Class. Name the file QRCodeModel.cs.
Add a Controller
-
In Solution Explorer, right-click Controllers > Add > Controller.
-
In the Add New Scaffolded Item dialog box, select MVC Controller - Empty > Add.
-
In the Add New Item - IronQRCode_Demo dialog, enter HomeController.cs and select Add.
I will use the existing HomeController to write the code.
![Add New Scaffolded Item]()
Add a View
- Right-click on the Views folder, then Add > New Folder and name the folder Home. I will use the existing Home folder.
- Right-click on the Views/Home folder, and then Add > New Item or choose View.
- Select MVC5 View from the Add new Scaffold item dialog.
- Enter View the name,
- Select Create template
- Select Model class QRCodeModel (IronQRCode_Demo.Models) from the Add view dialog
- Select Show All Templates.
- Select Add
- Replace the code in the CreateQRCode View.
![Add view]()
Run the Application
Select Ctrl+F5 to run the app without the debugger. Visual Studio runs the app and opens the default browser.
![Create QR Code]()
Conclusion
In conclusion, we have generated a QRCode image. We can save QRCode as JPG, PNG images, PDF, or HTML files in ASP.NET MVC. We can also add a logo to our QRCode file in ASP.NET MVC.