在实际环境中测试
在生产中测试无水印。
随时随地为您服务。
在当今快节奏的数字时代,生成二维码已成为高效信息共享的重要工具。 这些小巧的二维 BarCode 能够存储各种数据,包括 URL、文本、联系信息、产品详细信息等,在现代通信中发挥着举足轻重的作用。 将 QR 代码生成功能纳入 ASP.NET MVC 应用程序后,您就可以通过无缝生成 QR 代码、增强用户体验、简化交互和促进轻松的信息交流来增强用户能力。
如果您正在开发 ASP.NET MVC 应用程序,并希望加入 QR 代码生成功能、IronBarcode是一个可简化翻译过程的优秀库。 在本文中,我们将探讨如何使用 Iron BarCode 库在 ASP.NET MVC 中生成二维码。
IronBarcode 是一款功能强大、特性丰富的 QR 码生成和识别库,适用于 .NET 应用程序。 通过 IronBarcode,开发人员可以轻松地将条形码和 QR 码功能集成到他们的 ASP.NET MVC 项目中,包括生成 QR 码的功能。 该库提供了一套全面的工具和应用程序接口,可简化二维码的创建和定制过程,使开发人员能够根据自己的具体要求对二维码进行定制。
IronBarcode 广泛支持包括 QR 码在内的各种条形码类型,是需要 QR 码生成功能的项目的理想选择。 它可以让开发人员灵活地指定要编码的数据、控制生成的 QR 代码的大小和分辨率,甚至添加颜色和徽标等可视化样式元素。 该库可确保生成高质量的条形码,并对 QR 码外观的各个方面进行精确控制。
除了 QR 代码生成,IronBarcode 还包括强大的条形码读取和解码功能。 它支持扫描和提取二维码中的数据,使应用程序能够处理二维码中编码的信息。 该功能有利于需要进行条形码扫描和数据提取的场景,如库存管理、票务系统和移动应用程序。
现在,让我们在 ASP .NET core MVC Web 应用程序中创建一个生成二维码的项目。
在深入了解实施细节之前,让我们先确保您的 ASP.NET MVC 项目已经设置好并准备就绪。 无论您是开始一个新项目还是处理现有项目,下面概述的步骤都将指导您完成将 Iron BarCode 库集成到应用程序中的过程。 在我的案例中,我创建了一个新项目。
创建新项目的步骤如下:
打开 Microsoft Visual Studio 2022。
在开始页面,点击 "创建新项目",或从顶部菜单转到 "文件">"新建">"项目"。
在 "创建新项目 "窗口中,您将看到不同的项目模板供您选择。 选择项目模板 "ASP.NET Core 网络应用程序(模型视图控制器)"然后点击 "下一步"。
输入项目名称和地点。 在计算机上选择合适的位置保存项目文件。
选择所需的框架版本。 Visual Studio 通常建议使用最新的稳定版本,但您也可以根据需要选择不同的版本。 我选择了 .NET 7
根据您的要求定制任何其他项目设置,如身份验证选项或项目文件夹。
单击 "创建 "创建项目。
然后,Visual Studio 将生成项目文件并打开解决方案资源管理器,您可以在其中查看项目结构并开始编写代码。
现在,我们需要将 Iron BarCode Library 安装到应用程序中。
首先,在 Visual Studio 中打开 NuGet 包管理器控制台并运行以下命令:
Install-Package BarCode
此命令将安装 Iron BarCode 库并在您的项目中添加必要的引用。
现在,让我们编写一段代码来创建 QR 代码。
QrCode
模型在 Model 文件夹中创建一个 Model 类,并编写以下代码。
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
在您的 ASP.NET MVC 项目中,创建一个名为 QRCodeController 的新控制器。 为此,请右键单击项目结构中的 Controllers 文件夹,选择 "添加",然后选择 "控制器"。从可用选项中选择 "MVC Controller - Empty"。
在 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
这段代码设置了一个可以生成二维码的控制器。 当调用 "CreateQRCode "操作时,它会获取二维码文本、生成二维码图像、保存图像并在视图中提供图像的 URL 以供显示。 更多详情请见下表:
IWebHostEnvironment
参数,用于访问虚拟主机环境。QRCodeModel
参数。QRCodeWriter
类生成 QR 代码。ViewBag.QrCodeUri
属性中,以便在视图中使用。CreateQRCode
视图现在,要添加一个新视图,请右键单击 QrCodeController
类中的 CreateQRCode
操作方法。 选择 "添加视图",然后选择 "Razor View",接下来点击 "添加 "按钮。
新窗口将如下所示。
写入视图名称,选择模板 "创建",然后选择我们新创建的模型类 QrCodeModel
。 单击添加按钮。 将创建视图。 用以下代码替换您的视图。
@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
现在,让我们转到 program.cs 类,更改默认的控制器路由。
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}")
这将把默认路由从 "HomeController "改为我们的 "QrCode "控制器。
现在,编译并运行该项目。
在文本框中输入文本,然后点击创建按钮。 将创建 Qr 代码并显示在屏幕上,如下图所示。
现在,让我们通过添加注释文本和 QR 码值以及更改 QR 码颜色,为我们的条形码添加可视化样式。
在 CreateQrCode
动作方法中添加以下代码行。
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)
现在,运行该项目并生成 QR 代码。
在 ASP.NET MVC 中,集成 IronBarcode 非常简单。 它提供了一个用户友好型界面,使二维码的使用变得简单。 利用 IronBarcode,您可以通过添加 QR 码功能来增强您的应用程序,使用户能够方便地共享和访问信息。 IronBarcode 是一个有价值的库,它简化了在 ASP.NET MVC 中生成 qr 代码和读取 QR 代码的过程。 它使开发人员能够创建动态应用程序,利用二维码的强大功能实现高效的数据共享和检索。
Iron BarCode 可免费供个人使用,但若用于商业目的,您需要购买其商业许可附带免费试用. 如果您获得完整的Iron Suite. Iron Suite for .NET 是一个全面的 .NET 软件组件集合,旨在简化开发任务并增强功能。 它提供五个功能强大的库,包括 Iron BarCode、IronOCR, IronPDF, IronXL和Iron Webscrapper这些工具使开发人员能够无缝处理 BarCode、光学字符识别、PDF 处理、Excel 和 CSV 文件。 如果您选择购买全套 Iron Suite,您将以购买两套产品的价格获得全部五套产品。