How to Print a Barcode in ASP .NET

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
$vbLabelText   $csharpLabel
// 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
$vbLabelText   $csharpLabel
@* 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

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he says it’s one of his favorite aspects of working with Iron Software. Jordi grew up in Miami, Florida and studied Computer Science and Statistics at University of Florida.
< PREVIOUS
How to use a Barcode Scanner in a C# Windows Application
NEXT >
How to Generate a QR Code Using C# with IronBarcode