How to Print a Barcode in ASP .NET

In this tutorial, we explore the process of printing barcodes within an ASP.NET web application using the Iron Barcode Library. The tutorial is conducted using Visual Studio 2022. It starts by navigating through the barcode model, which includes properties for file name and barcode content. Then, the tutorial explains the controller setup responsible for generating barcodes using the Iron Barcode Library. The generated barcode is saved as a JPEG file, and any exceptions redirect the user back to the barcode creation view.

The tutorial further examines the createbarcode.cshtml file, which contains a form for user input. Users can enter a file name and content, and upon submission, the server generates the barcode. If successfully generated, the barcode image displays, otherwise, the user is redirected back to input details. Finally, the tutorial demonstrates running the project and generating the barcode. The Iron Barcode Library proves to be a valuable tool for barcode generation in ASP.NET applications. The tutorial concludes with a call to subscribe for more modern software tutorials.

Example Code

Below is an example code snippet for the controller setup to generate a barcode using the Iron Barcode Library in an ASP.NET application.

using IronBarCode;
using Microsoft.AspNetCore.Mvc;
using System;

namespace BarcodeApp.Controllers
{
    public class BarcodeController : Controller
    {
        // GET: /Barcode/Create
        public IActionResult Create()
        {
            return View();
        }

        // POST: /Barcode/Create
        [HttpPost]
        public IActionResult Create(string fileName, string barcodeContent)
        {
            try
            {
                // Generate a barcode
                BarcodeWriter.CreateBarcode(barcodeContent, BarcodeEncoding.Code128)
                             .ResizeTo(300, 100)
                             .SaveAsJpeg($"{fileName}.jpeg");

                // Return view for successful barcode generation
                ViewBag.Message = "Barcode created successfully!";
                return View("DisplayBarcode", $"{fileName}.jpeg");
            }
            catch (Exception ex)
            {
                // Log the exception (ex) if necessary
                // Redirect back to Create view on failure
                ViewBag.ErrorMessage = "Error creating barcode. Please try again.";
                return View();
            }
        }
    }
}
using IronBarCode;
using Microsoft.AspNetCore.Mvc;
using System;

namespace BarcodeApp.Controllers
{
    public class BarcodeController : Controller
    {
        // GET: /Barcode/Create
        public IActionResult Create()
        {
            return View();
        }

        // POST: /Barcode/Create
        [HttpPost]
        public IActionResult Create(string fileName, string barcodeContent)
        {
            try
            {
                // Generate a barcode
                BarcodeWriter.CreateBarcode(barcodeContent, BarcodeEncoding.Code128)
                             .ResizeTo(300, 100)
                             .SaveAsJpeg($"{fileName}.jpeg");

                // Return view for successful barcode generation
                ViewBag.Message = "Barcode created successfully!";
                return View("DisplayBarcode", $"{fileName}.jpeg");
            }
            catch (Exception ex)
            {
                // Log the exception (ex) if necessary
                // Redirect back to Create view on failure
                ViewBag.ErrorMessage = "Error creating barcode. Please try again.";
                return View();
            }
        }
    }
}
Imports IronBarCode
Imports Microsoft.AspNetCore.Mvc
Imports System

Namespace BarcodeApp.Controllers
	Public Class BarcodeController
		Inherits Controller

		' GET: /Barcode/Create
		Public Function Create() As IActionResult
			Return View()
		End Function

		' POST: /Barcode/Create
		<HttpPost>
		Public Function Create(ByVal fileName As String, ByVal barcodeContent As String) As IActionResult
			Try
				' Generate a barcode
				BarcodeWriter.CreateBarcode(barcodeContent, BarcodeEncoding.Code128).ResizeTo(300, 100).SaveAsJpeg($"{fileName}.jpeg")

				' Return view for successful barcode generation
				ViewBag.Message = "Barcode created successfully!"
				Return View("DisplayBarcode", $"{fileName}.jpeg")
			Catch ex As Exception
				' Log the exception (ex) if necessary
				' Redirect back to Create view on failure
				ViewBag.ErrorMessage = "Error creating barcode. Please try again."
				Return View()
			End Try
		End Function
	End Class
End Namespace
$vbLabelText   $csharpLabel

Razor View (createbarcode.cshtml)

The following code sets up the Razor view with a form for inputting the file name and barcode content:

@{
    ViewData["Title"] = "Create Barcode";
}

<h2>Create Barcode</h2>

<form method="post" asp-controller="Barcode" asp-action="Create">
    <div>
        <label for="fileName">File Name</label>
        <input type="text" id="fileName" name="fileName" required />
    </div>
    <div>
        <label for="barcodeContent">Barcode Content</label>
        <input type="text" id="barcodeContent" name="barcodeContent" required />
    </div>
    <button type="submit">Generate Barcode</button>
</form>

@if (ViewBag.Message != null)
{
    <p>@ViewBag.Message</p>
}

@if (ViewBag.ErrorMessage != null)
{
    <p style="color: red;">@ViewBag.ErrorMessage</p>
}
@{
    ViewData["Title"] = "Create Barcode";
}

<h2>Create Barcode</h2>

<form method="post" asp-controller="Barcode" asp-action="Create">
    <div>
        <label for="fileName">File Name</label>
        <input type="text" id="fileName" name="fileName" required />
    </div>
    <div>
        <label for="barcodeContent">Barcode Content</label>
        <input type="text" id="barcodeContent" name="barcodeContent" required />
    </div>
    <button type="submit">Generate Barcode</button>
</form>

@if (ViewBag.Message != null)
{
    <p>@ViewBag.Message</p>
}

@if (ViewBag.ErrorMessage != null)
{
    <p style="color: red;">@ViewBag.ErrorMessage</p>
}
HTML

Further Reading

For more detailed instructions and additional examples, you can refer to the article: 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.
NEXT >
How to Generate a QR Code Using C# with IronBarcode

Ready to get started? Version: 2025.6 just released

View Licenses >