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 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.
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
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>
}
For more detailed instructions and additional examples, you can refer to the article: How to Print a Barcode in ASP .NET