Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
In today's fast-paced digital landscape, the generation of QR codes has become an essential tool for efficient information sharing. These compact, two-dimensional barcodes, capable of storing a wide range of data, including URLs, text, contact information, product details, and much more, play a pivotal role in modern communication. Incorporating QR code generation capabilities into your ASP.NET MVC application allows you to empower users by seamlessly generating QR codes and enhancing their experience, streamlining interactions, and facilitating the effortless exchange of information.
If you're developing an ASP.NET MVC application and want to incorporate QR code generation capabilities, IronBarcode is an excellent library that simplifies the process. In this article, we will explore how to generate QR codes in ASP.NET MVC using the Iron Barcode library.
IronBarcode is a powerful and feature-rich QR Code generation and recognition library for .NET applications. With IronBarcode, developers can easily integrate barcode and QR Code functionality into their ASP.NET MVC projects, including the ability to generate QR codes. The library provides a comprehensive set of tools and APIs that simplify the process of creating and customizing QR codes, allowing developers to tailor them to their specific requirements.
IronBarcode offers extensive support for various barcode types, including QR codes, making it an ideal choice for projects that require QR code generation capabilities. It provides developers with the flexibility to specify the data to be encoded, control the size and resolution of the generated QR codes, and even add visual styling elements such as colors and logos. The library ensures high-quality barcode generation with precise control over every aspect of the QR code's appearance.
Beyond QR code generation, IronBarcode also includes robust features for barcode reading and decoding. It supports scanning and extracting data from QR codes, enabling applications to process information encoded within them. This functionality is beneficial for scenarios where barcode scanning and data extraction are necessary, such as inventory management, ticketing systems, and mobile applications.
Now, Let's create a project to Generate a QR code in the ASP .NET core MVC Web app.
Before we delve into the implementation details, let's ensure that your ASP.NET MVC project is set up and ready to go. Whether you're starting a new project or working with an existing one, the steps outlined below will guide you through the process of integrating the Iron Barcode library into your application. In my case, I have created a new project.
Step for creating a new project is as:
Visual Studio will then generate the project files and open the solution explorer, where you can see the project structure and start working on your code.
Now, we need to install Iron Barcode Library into our application.
To begin, open the NuGet Package Manager Console in Visual Studio and run the following command:
Install-Package BarCode
This command will install the Iron Barcode library and adds the necessary references to your project.
Now, Let's write a code to create QR codes.
QrCode
ModelCreate a Model Class inside the Model folder, and write the following code.
public class QRCodeModel
{
[Display(Name = "Enter QR Code Text")]
public string QRCodeText
{
get;
set;
}
}
public class QRCodeModel
{
[Display(Name = "Enter QR Code Text")]
public string QRCodeText
{
get;
set;
}
}
Public Class QRCodeModel
<Display(Name := "Enter QR Code Text")>
Public Property QRCodeText() As String
End Class
In your ASP.NET MVC project, create a new controller named QRCodeController. To do this, Right-click on the Controllers folder in your project's structure, select "Add," and then choose "Controller." From the available options, select "MVC Controller - Empty".
Write the following code in QrCodeController
:
public class QrCodeController : Controller
{
private readonly IWebHostEnvironment _environment;
public QrCodeController(IWebHostEnvironment environment)
{
_environment = environment;
}
public IActionResult CreateQRCode()
{
return View();
}
[HttpPost]
public IActionResult CreateQRCode(QRCodeModel generateQRCode)
{
try
{
// Creating QR Code
GeneratedBarcode barcode = QRCodeWriter.CreateQrCode(generateQRCode.QRCodeText);
string path = Path.Combine(_environment.WebRootPath, "GeneratedQRCode");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string filePath = Path.Combine(_environment.WebRootPath, "GeneratedQRCode/qrcode.png");
barcode.SaveAsPng(filePath);
string fileName = Path.GetFileName(filePath); // qr code file
string imageUrl = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}" + "/GeneratedQRCode/" + fileName;
ViewBag.QrCodeUri = imageUrl;
}
catch (Exception)
{
throw;
}
return View();
}
}
public class QrCodeController : Controller
{
private readonly IWebHostEnvironment _environment;
public QrCodeController(IWebHostEnvironment environment)
{
_environment = environment;
}
public IActionResult CreateQRCode()
{
return View();
}
[HttpPost]
public IActionResult CreateQRCode(QRCodeModel generateQRCode)
{
try
{
// Creating QR Code
GeneratedBarcode barcode = QRCodeWriter.CreateQrCode(generateQRCode.QRCodeText);
string path = Path.Combine(_environment.WebRootPath, "GeneratedQRCode");
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
string filePath = Path.Combine(_environment.WebRootPath, "GeneratedQRCode/qrcode.png");
barcode.SaveAsPng(filePath);
string fileName = Path.GetFileName(filePath); // qr code file
string imageUrl = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}" + "/GeneratedQRCode/" + fileName;
ViewBag.QrCodeUri = imageUrl;
}
catch (Exception)
{
throw;
}
return View();
}
}
Public Class QrCodeController
Inherits Controller
Private ReadOnly _environment As IWebHostEnvironment
Public Sub New(ByVal environment As IWebHostEnvironment)
_environment = environment
End Sub
Public Function CreateQRCode() As IActionResult
Return View()
End Function
<HttpPost>
Public Function CreateQRCode(ByVal generateQRCode As QRCodeModel) As IActionResult
Try
' Creating QR Code
Dim barcode As GeneratedBarcode = QRCodeWriter.CreateQrCode(generateQRCode.QRCodeText)
Dim path As String = System.IO.Path.Combine(_environment.WebRootPath, "GeneratedQRCode")
If Not Directory.Exists(path) Then
Directory.CreateDirectory(path)
End If
Dim filePath As String = System.IO.Path.Combine(_environment.WebRootPath, "GeneratedQRCode/qrcode.png")
barcode.SaveAsPng(filePath)
Dim fileName As String = System.IO.Path.GetFileName(filePath) ' qr code file
Dim imageUrl As String = $"{Me.Request.Scheme}://{Me.Request.Host}{Me.Request.PathBase}" & "/GeneratedQRCode/" & fileName
ViewBag.QrCodeUri = imageUrl
Catch e1 As Exception
Throw
End Try
Return View()
End Function
End Class
This code sets up a controller that can generate QR codes. When the CreateQRCode
action is called, it takes the text for the QR code, generates the QR code image, saves it, and provides the URL for the image in the view for display. More detail is as:
IWebHostEnvironment
parameter to access the web hosting environment.CreateQRCode
action returns a view.CreateQRCode
action with the [HttpPost
] attribute takes a QRCodeModel
parameter, which contains the QR code text.QRCodeWriter
class from the Iron Barcode library.GeneratedQRCode
in the web root path.GeneratedQRCode
folder doesn't exist, it is created.ViewBag.QrCodeUri
property to be used in the view.CreateQRCode
ViewNow, to add a new view, right-click on the CreateQRCode
action method in QrCodeController
class. Select "Add View" then select "Razor View" Next click on the "Add" button.
A new window will appear as shown below.
Write view Name, Select template "Create", and Select our newly created model class QrCodeModel
. Click on Add Button. The view will be created. Replace your view with the below code.
@model QRCodeMVC.Models.QRCodeModel
@{
ViewData ["Title"] = "CreateQRCode";
}
<h1>Create QRCode in ASP.NET MVC</h1>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="CreateQRCodeView">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="QRCodeText" class="control-label"></label>
<input asp-for="QRCodeText" class="form-control" />
<span asp-validation-for="QRCodeText" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
<div class="form-group">
<img src="@ViewBag.QrCodeUri" class="img-thumbnail" />
</div>
</form>
</div>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
@model QRCodeMVC.Models.QRCodeModel
@{
ViewData ["Title"] = "CreateQRCode";
}
<h1>Create QRCode in ASP.NET MVC</h1>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="CreateQRCodeView">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="QRCodeText" class="control-label"></label>
<input asp-for="QRCodeText" class="form-control" />
<span asp-validation-for="QRCodeText" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
<div class="form-group">
<img src="@ViewBag.QrCodeUri" class="img-thumbnail" />
</div>
</form>
</div>
</div>
@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
model ReadOnly Property () As QRCodeMVC.Models.QRCodeModel
ViewData ("Title") = "CreateQRCode"
End Property
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: <h1> Create QRCode in ASP.NET MVC</h1> <hr /> <div class="row"> <div class="col-md-4"> <form asp-action="CreateQRCodeView"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class="control-label"></label> <input asp-for="QRCodeText" class="form-control" /> <span asp-validation-for="QRCodeText" class="text-danger"></span> </div> <div class="form-group"> <input type="submit" value="Create" class="btn btn-primary" /> </div> <div class="form-group"> <img src="@ViewBag.QrCodeUri" class="img-thumbnail" /> </div> </form> </div> </div> @section Scripts
"@ViewBag.QrCodeUri" class="img-thumbnail" /> </div> </form> </div> </div> section Scripts
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Friend <h1> Create QRCode in ASP.NET MVC</h1> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCodeView"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class="control-label"></label> <input asp-for="QRCodeText" class="form-control" /> <span asp-validation-for="QRCodeText" class="text-danger"></span> </div> <div class="form-group"> <input type="submit" value="Create" class="btn btn-primary" /> </div> <div class="form-group"> <img src="@ViewBag.QrCodeUri" class
"form-group"> <img src="@ViewBag.QrCodeUri" class
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Friend <h1> Create QRCode in ASP.NET MVC</h1> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCodeView"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class="control-label"></label> <input asp-for="QRCodeText" class="form-control" /> <span asp-validation-for="QRCodeText" class="text-danger"></span> </div> <div class="form-group"> <input type="submit" value="Create" class="btn btn-primary" /> </div> <div class="form-group"> <img src
"btn btn-primary" /> </div> <div class="form-group"> <img src
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Friend <h1> Create QRCode in ASP.NET MVC</h1> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCodeView"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class="control-label"></label> <input asp-for="QRCodeText" class="form-control" /> <span asp-validation-for="QRCodeText" class="text-danger"></span> </div> <div class="form-group"> <input type="submit" value="Create" class="btn btn-primary" /> </div> <div class
"Create" class="btn btn-primary" /> </div> <div class
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Friend <h1> Create QRCode in ASP.NET MVC</h1> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCodeView"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class="control-label"></label> <input asp-for="QRCodeText" class="form-control" /> <span asp-validation-for="QRCodeText" class="text-danger"></span> </div> <div class="form-group"> <input type="submit" value="Create" class
"submit" value="Create" class
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Private Friend <h1> Create QRCode in ASP.NET MVC</h1> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCodeView"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class="control-label"></label> <input asp-for="QRCodeText" class="form-control" /> <span asp-validation-for="QRCodeText" class="text-danger"></span> </div> <div class="form-group"> <input type="submit" value
"form-group"> <input type="submit" value
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Private Private Friend <h1> Create QRCode in ASP.NET MVC</h1> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCodeView"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class="control-label"></label> <input asp-for="QRCodeText" class="form-control" /> <span asp-validation-for="QRCodeText" class="text-danger"></span> </div> <div class="form-group"> <input type
"text-danger"></span> </div> <div class="form-group"> <input type
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Private Private Private Friend <h1> Create QRCode in ASP.NET MVC</h1> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCodeView"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class="control-label"></label> <input asp-for="QRCodeText" class="form-control" /> <span asp-validation-for="QRCodeText" class="text-danger"></span> </div> <div class
"QRCodeText" class="text-danger"></span> </div> <div class
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Private Private Private Private Friend <h1> Create QRCode in ASP.NET MVC</h1> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCodeView"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class="control-label"></label> <input asp-for="QRCodeText" class="form-control" /> <span asp-validation-for="QRCodeText" class
"form-control" /> <span asp-validation-for="QRCodeText" class
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Private Private Private Private Private Friend <h1> Create QRCode in ASP.NET MVC</h1> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCodeView"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class="control-label"></label> <input asp-for="QRCodeText" class="form-control" /> <span asp-validation-for
"QRCodeText" class="form-control" /> <span asp-validation-for
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Private Private Private Private Private Private Friend <h1> Create QRCode in ASP.NET MVC</h1> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCodeView"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class="control-label"></label> <input asp-for="QRCodeText" class
"control-label"></label> <input asp-for="QRCodeText" class
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Private Private Private Private Private Private Private Friend <h1> Create QRCode in ASP.NET MVC</h1> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCodeView"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class="control-label"></label> <input asp-for
"QRCodeText" class="control-label"></label> <input asp-for
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Private Private Private Private Private Private Private Private Friend <h1> Create QRCode in ASP.NET MVC</h1> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCodeView"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for="QRCodeText" class
"form-group"> <label asp-for="QRCodeText" class
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Private Private Private Private Private Private Private Private Private Friend <h1> Create QRCode in ASP.NET MVC</h1> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCodeView"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class="form-group"> <label asp-for
"text-danger"></div> <div class="form-group"> <label asp-for
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Private Private Private Private Private Private Private Private Private Private Friend <h1> Create QRCode in ASP.NET MVC</h1> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCodeView"> <div asp-validation-summary="ModelOnly" class="text-danger"></div> <div class
"ModelOnly" class="text-danger"></div> <div class
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Private Private Private Private Private Private Private Private Private Private Private Friend <h1> Create QRCode in ASP.NET MVC</h1> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCodeView"> <div asp-validation-summary="ModelOnly" class
"CreateQRCodeView"> <div asp-validation-summary="ModelOnly" class
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Private Private Private Private Private Private Private Private Private Private Private Private Friend <h1> Create QRCode in ASP.NET MVC</h1> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action="CreateQRCodeView"> <div asp-validation-summary
"col-md-4"> <form asp-action="CreateQRCodeView"> <div asp-validation-summary
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: Private Private Private Private Private Private Private Private Private Private Private Private Private Private Private Private Friend <h1> Create QRCode in ASP.NET MVC</h1> <hr /> <div Class="row"> <div class="col-md-4"> <form asp-action
"row"> <div class="col-md-4"> <form asp-action
'INSTANT VB WARNING: Instant VB cannot determine whether both operands of this division are integer types - if they are then you should use the VB integer division operator:
Private Private Private Private Private Private Private Private Private Private Private Private Private Private Private Private Private Private Friend (Of h1) Create QRCode in ASP.NET MVC</h1> <hr /> <div Class="row"> <div class
@
If True Then
Await Html.RenderPartialAsync("_ValidationScriptsPartial")
End If
End Class
Now, Let's move to the program.cs class and change the default Controller Route.
app.MapControllerRoute(
name: "default",
pattern: "{controller=QrCode}/{action=CreateQRCode}"
);
app.MapControllerRoute(
name: "default",
pattern: "{controller=QrCode}/{action=CreateQRCode}"
);
app.MapControllerRoute(name:= "default", pattern:= "{controller=QrCode}/{action=CreateQRCode}")
This will change the default route from HomeController
to our QrCode
Controller.
Now, Compile and run the project.
Enter text in the text box and click on create button. Qr code will be created and displayed on the screen as shown below.
Now, let's add visual styling to our barcode by adding Annotation Text, and QR Code value, and Changing the QR Code color.
Add the following line of code inside the CreateQrCode
Action method.
barcode.AddAnnotationTextAboveBarcode("QR Code Generated by Iron PDF");
barcode.AddBarcodeValueTextBelowBarcode();
barcode.ChangeBackgroundColor(Color.White);
barcode.ChangeBarCodeColor(Color.MediumVioletRed);
barcode.AddAnnotationTextAboveBarcode("QR Code Generated by Iron PDF");
barcode.AddBarcodeValueTextBelowBarcode();
barcode.ChangeBackgroundColor(Color.White);
barcode.ChangeBarCodeColor(Color.MediumVioletRed);
barcode.AddAnnotationTextAboveBarcode("QR Code Generated by Iron PDF")
barcode.AddBarcodeValueTextBelowBarcode()
barcode.ChangeBackgroundColor(Color.White)
barcode.ChangeBarCodeColor(Color.MediumVioletRed)
Now, run the project and generate the QR Code.
In ASP.NET MVC, integrating IronBarcode is straightforward. It provides a user-friendly interface, making it easy to work with QR codes. By leveraging IronBarcode, you can enhance your application by adding QR code functionality, enabling users to share and access information conveniently. IronBarcode is a valuable library that simplifies the process of generating qr code and reading QR codes in ASP.NET MVC. It empowers developers to create dynamic applications that utilize the power of QR codes for efficient data sharing and retrieval.
Iron Barcode is free for personal use, However, for commercial purposes, you need to buy its commercial license which comes with a free trial. You may also get a significant discount if you get a complete Iron Suite. Iron Suite is a comprehensive collection of .NET software components designed to simplify development tasks and enhance functionality. It offers five powerful libraries, including Iron Barcode, IronOCR, IronPDF, IronXL, and Iron Webscrapper that enable developers to work with barcodes, optical character recognition, PDF processing, Excel, and CSV files seamlessly. You will get all five products for the price of two if you opt to purchase the complete Iron Suite.
9 .NET API products for your office documents