Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
In this tutorial, we delve into the process of printing barcodes in an ASP.NET web application using the Iron Barcode library. The demonstration utilizes Visual Studio 2022 and the Iron Barcode library, which can be installed via the NuGet Package Manager. We start by accessing the barcode model in the barcodeModel.cs
file, where a public class contains properties for file name and barcode content. This model is then used in the barcodeController.cs
file, which manages the creation and display of barcodes. The createBarcode
method uses the Iron Barcode library to generate a JPEG file of the barcode, handling exceptions by redirecting users to a retry view if needed. In the createbarcode.cshtml
file, users input details to generate the barcode, and upon clicking 'Create', the server processes this data to output a barcode image. Any issues redirect users back to the input view. This tutorial highlights Iron Barcode's effectiveness and ease of use in ASP.NET applications, making it an essential tool for developers. Subscribe for more tutorials on modern software development.
// barcodeModel.cs
public class BarcodeModel
{
// Property to store the file name of the barcode
public string FileName { get; set; }
// Property to store the content from which the barcode will be generated
public string Content { get; set; }
}
// barcodeModel.cs
public class BarcodeModel
{
// Property to store the file name of the barcode
public string FileName { get; set; }
// Property to store the content from which the barcode will be generated
public string Content { get; set; }
}
' barcodeModel.cs
Public Class BarcodeModel
' Property to store the file name of the barcode
Public Property FileName() As String
' Property to store the content from which the barcode will be generated
Public Property Content() As String
End Class
// barcodeController.cs
using IronBarCode;
using Microsoft.AspNetCore.Mvc;
public class BarcodeController : Controller
{
// Action method to create a barcode
public IActionResult CreateBarcode(BarcodeModel model)
{
try
{
// Generates a barcode image from the content provided in the model
var barcode = BarcodeWriter.CreateBarcode(model.Content, BarcodeEncoding.Code128);
// Saves the generated barcode image as a JPEG file at a specified path
barcode.SaveAsJpeg($"wwwroot/images/{model.FileName}.jpeg");
// Redirects to a view that confirms barcode creation
return View("BarcodeCreated");
}
catch (Exception)
{
// Redirects to a retry view if barcode creation fails
return View("Retry");
}
}
}
// barcodeController.cs
using IronBarCode;
using Microsoft.AspNetCore.Mvc;
public class BarcodeController : Controller
{
// Action method to create a barcode
public IActionResult CreateBarcode(BarcodeModel model)
{
try
{
// Generates a barcode image from the content provided in the model
var barcode = BarcodeWriter.CreateBarcode(model.Content, BarcodeEncoding.Code128);
// Saves the generated barcode image as a JPEG file at a specified path
barcode.SaveAsJpeg($"wwwroot/images/{model.FileName}.jpeg");
// Redirects to a view that confirms barcode creation
return View("BarcodeCreated");
}
catch (Exception)
{
// Redirects to a retry view if barcode creation fails
return View("Retry");
}
}
}
' barcodeController.cs
Imports IronBarCode
Imports Microsoft.AspNetCore.Mvc
Public Class BarcodeController
Inherits Controller
' Action method to create a barcode
Public Function CreateBarcode(ByVal model As BarcodeModel) As IActionResult
Try
' Generates a barcode image from the content provided in the model
Dim barcode = BarcodeWriter.CreateBarcode(model.Content, BarcodeEncoding.Code128)
' Saves the generated barcode image as a JPEG file at a specified path
barcode.SaveAsJpeg($"wwwroot/images/{model.FileName}.jpeg")
' Redirects to a view that confirms barcode creation
Return View("BarcodeCreated")
Catch e1 As Exception
' Redirects to a retry view if barcode creation fails
Return View("Retry")
End Try
End Function
End Class
@* createbarcode.cshtml *@
@model BarcodeModel
<form asp-action="CreateBarcode" method="post">
<div>
<label asp-for="FileName">File Name:</label>
<input asp-for="FileName" />
</div>
<div>
<label asp-for="Content">Barcode Content:</label>
<input asp-for="Content" />
</div>
<button type="submit">Create</button>
</form>
Further Reading: How to Print a Barcode in ASP .NET