In the following step by step tutorial, we'll show you how to quickly create a new ASP.NET Core MVC web app, and integrate online barcode generating feature on ASP.NET web page using C# codes.
1. Create a new ASP.NET Core Model-View-Controller (MVC) web app
Here we will quickly create a new ASP.NET Core web app using Visual Studio .NET
Start Visual Studio and select Create a new project
In the Create a new project dialog, select ASP.NET Core Web App (Model-View-Controller), and select button Next.
In the Configure your new project dialog, enter BarcodeLib.Create.ASPNETCoreMVCDemo for Project name. It is important to
name the proejct BarcodeLib.Create.ASPNETCoreMVCDemo, including letter case, so the namespaces will match when you copy, paste the following
C# example code.
Select button Create
In the Additional information diaplog, select .NET 6.0 or .NET 7.0 for the Framework dropdowns. Select button Create.
Now we have created a new ASP.NET Core MVC web project successfully, and we can find the build-in MVC views and controllers generated in the new ASP.NET Core project.
2. Install C# Barcode library and Nuget package
Now we will add BarcodeLib C# barcode generator library to the asp.net core project and also add a Nuget package named "System.Drawing.Common" from Microsoft.
Add C# Barcode DLL
In the Solution Explorer window, right click Dependencies, select Add Project Reference...
Select button Browse... in the Reference Manager dialog.
Choose dll file BarcodeLib.Barcode.Common.dll from BarcodeLib downloaded package /Dll/net-standard/
Check BarcodeLib.Barcode.Common.dll is selected, and select button OK
Now we have added the barcodelib barcode generator library dll. We will start to add a library "System.Drawing.Common" from Nuget
Install NuGet Package
Open Manage NuGet Packages... from Visual Studio
Enter System.Drawing.Common to search NuGet package
Select and install the System.Drawing.Common by Microsoft
Now we have installed the C# barcode library and nuget package successfully.
3. Add C# codes to MVC Controller
Here we add C# source codes to MVC controller HomeController.cs
usingBarcodeLib.Barcode;usingBarcodeLib.Create.ASPNETCoreMVCDemo.Models;usingMicrosoft.AspNetCore.Mvc;usingSystem.Diagnostics;usingSystem.Drawing;usingSystem.Drawing.Imaging;namespaceOnBarcode.QRCodeGenerator.ASPNETCoreMVCDemo.Controllers{publicclassHomeController:Controller{privatestaticStringtbData="QR Code";privatestaticStringtbBarcodeWidth="3";privatestaticStringtbResolution="300";privatestaticStringtbBarColor="000000";privatestaticStringtbBackgroundColor="FFFFFF";publicfloatBarcodeWebDisplayWidth{get;set;}=0f;privatereadonlyILogger<HomeController>_logger;publicHomeController(ILogger<HomeController>logger){_logger=logger;}publicIActionResultIndex(){ViewData["Data"]=tbData;ViewData["BarcodeWidth"]=tbBarcodeWidth;ViewData["Resolution"]=tbResolution;ViewData["BarColor"]=tbBarColor;ViewData["BackgroundColor"]=tbBackgroundColor;returnView();}publicIActionResultPrivacy(){returnView();} [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]publicIActionResultError(){returnView(newErrorViewModel{RequestId=Activity.Current?.Id??HttpContext.TraceIdentifier});}// publicTask<IActionResult>BarcodeRender(){try{tbData=Request.Form["tbData"].ToString();tbBarcodeWidth=Request.Form["tbBarcodeWidth"].ToString();tbResolution=Request.Form["tbResolution"].ToString();tbBarColor=Request.Form["tbBarColor"].ToString();tbBackgroundColor=Request.Form["tbBackgroundColor"].ToString();byte[]dataBytes=tryToCreateBarcode();if(dataBytes==null||dataBytes.Length==0)thrownewException("Fail to encode QR Code");ViewData["BarcodeImage"]=System.Convert.ToBase64String(dataBytes);ViewData["BarcodeWebDisplayWidth"]=BarcodeWebDisplayWidth;ViewData["ViewResult"]="Succeed";returnTask.FromResult<IActionResult>(View());}catch(Exception){ViewData["ViewResult"]="Failed";returnTask.FromResult<IActionResult>(View());}}privatebyte[]tryToCreateBarcode(){try{if(String.IsNullOrEmpty(tbData))thrownewException("QR Code data cannot be null or empty.");intimageDpi=Convert.ToInt32(tbResolution);if(imageDpi>1000)thrownewException("QR Code image resolution is out of range.");floatwidth=Convert.ToSingle(tbBarcodeWidth);if(width>8F)thrownewException("QR Code width is out of range.");QRCodebarcode=newQRCode();barcode.Data=tbData;barcode.UOM=UnitOfMeasure.INCH;barcode.ImageWidth=width;barcode.ImageHeight=width;BarcodeWebDisplayWidth=width*96f;barcode.Resolution=imageDpi;barcode.ModuleColor=Color.FromArgb((byte)(HexToInt(tbBarColor[0])*16+HexToInt(tbBarColor[1])),(byte)(HexToInt(tbBarColor[2])*16+HexToInt(tbBarColor[3])),(byte)(HexToInt(tbBarColor[4])*16+HexToInt(tbBarColor[5])));barcode.BackgroundColor=Color.FromArgb((byte)(HexToInt(tbBackgroundColor[0])*16+HexToInt(tbBackgroundColor[1])),(byte)(HexToInt(tbBackgroundColor[2])*16+HexToInt(tbBackgroundColor[3])),(byte)(HexToInt(tbBackgroundColor[4])*16+HexToInt(tbBackgroundColor[5])));barcode.ImageFormat=ImageFormat.Png;returnbarcode.drawBarcodeAsBytes();}catch(Exception){returnnewbyte[0];}}publicstaticintHexToInt(charc){if(c>='0'&&c<='9')return(int)c-(int)'0';if(c>='A'&&c<='F')return(int)c-(int)'A'+10;if(c>='a'&&c<='f')return(int)c-(int)'a'+10;return0;}}}
4. Add C# codes to MVC Views
Here we add C# source codes to MVC view Index.cshtml from /Views/Home/
Now you have completed the ASP.NET Core MVC demo app, and it allows the web users to create and customize QR Code online in the ASP.NET MVC web app.
Run the ASP.NET Core MVC web app directly from Visual Studio. You can generate QR Code 2d barcodes online in web browser.