C# Barcode Generator in ASP.NET Core web application

How to create, print barcode label images for ASP.NET Core web applications with free C# barcode example source code

Using Free C# Souce Code to Generate Barcode Labels for ASP.NET Web Application & IIS Projects



This C# tutorial provides a basic working experience for building an ASP.NET Core web app, where user can input barcode data message, and generate Code 128 barcode on page. After this tutorial, you will be albe to

  • Create 20+ barcodes in ASP.NET Core Razor pages, MVC, Blazor web application
  • Customize the barcode with ISO standard specified property settings
  • Generate barcode with international text characters
  • Support both System.Drawing.Common and SkiaSharp
  • Support .NET 7, 6, 5 and .NET Core 3.1, 2.1 ASP.NET Core web application

How to generate linear, 2d barcodes in ASP.NET Core web app using C#

  1. Download .NET Barcode Generator Suite
  2. Install C# library to create barcodes in ASP.NET Core
  3. Step by Step Tutorial












Prerequisites

Top
Download and install the following software on your computer
  1. Visual Studio 2022
  2. .NET 6.0 SDK
  3. OnBarcode.Barcode.Common.Skia.dll

Create an ASP.NET Core web app with barcode generation on page using C#

Top

1. Create new ASP.NET Core web app



Start Visual Studio 2022 and select "Create a new project".





Select "ASP.NET Core Web App" in the dialog, and then press "Next" button.





Create a new project with name "OnBarcode.BarcodeGenerator.ASPNETCoreDemo".





Select .NET 6.0 (Long-term support), and then press "Create"





Now, all auto-generated files could be found in the solution explorer.





Open the file "Program.cs" and ensure that it has enabled following Middlewares.

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.Run();


2. Install SkiaSharp and OnBarcode Barcode Generator dll



Right-click "Dependencies" in the Solution Explorer, and select "Manage NuGet Packages".





Select "Browse" and use the search control to find "SkiaSharp" from the package source "nugget.org".





Choose the latest stable version to install SkiaSharp.





Check "Packages" in the Solution Explorer to ensure the installation is success.





Add DLL reference "OnBarcode.Barcode.Common.Skia.dll".





Add C# code to generate barcode in ASP.NET web page



Copy all following codes to replace the content of the file "Index.cshtml".
@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<div class="text-center">
    <form asp-page="./Index" method="post">
        <br />
        Barcode Message:  
        <input type="text" id="tb1" name="tbMessage" style="width: 600px; " asp-for="@Model.Message" />
        <br />
        <br />
        <input type="submit" value="Create Barcode" />
    </form>
    <br />
    <img src="data:image/png;base64,@Model.BarcodeImage" />
</div>


Copy all following codes to replace the content in the file "Index.cshtml.cs".
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace OnBarcode.BarcodeGenerator.ASPNETCoreDemo.Pages
{
    public class IndexModel : PageModel
    {
        public String Message { get; set; } = "";
        public String BarcodeImage { get; set; } = "";

        public void OnGet()
        {
        }

        public Task<IActionResult> OnPost()
        {
            this.Message = Request.Form["tbMessage"].ToString();

            OnBarcode.Barcode.Linear linear = new OnBarcode.Barcode.Linear();
            linear.Type = OnBarcode.Barcode.BarcodeType.CODE128;
            linear.Data = this.Message;
            linear.Format = SkiaSharp.SKEncodedImageFormat.Png;

            byte[] dataBytes = linear.drawBarcodeAsBytes();
            BarcodeImage = System.Convert.ToBase64String(dataBytes);

            return Task.FromResult<IActionResult>(Page());
        }
    }
}


4. Run web app



It is done. Now press "Ctrl+F5" to run the project.



It launches the Kestrel server and opens the default browser.
OnBarcode is a market-leading provider of barcode imaging generator, reader controls and components for ASP.NET, Windows Forms, WPF, as well Java, Android, iOS (iPhone, iPad) across all major enterprise development platforms. We provides comprehensive tutorials and how-tos for various linear, 2d barcode information, such as C# in ASP.NET, C# .NET, C# Barcode Encoding, C# Barcode Image, VB.NET in ASP.NET, VB.NET Winforms, VB.NET Barcode Encoding. OnBarcode barcode products are supported by RasterEdge ASP.NET Document Viewer, which supports ASP.NET PDF Viewer, ASP.NET MVC Document Viewer, ASP.NET PDF Editor, MVC PDF Viewer. And provide high quality C# Convert PDF to Tiff, C# Convert PDF to Word, C# Convert PDF to HTML, C# Convert PDF to Jpeg images, and their easy and simple documents, like C# PDF SDK, C# extract text from PDF, C# Compress PDF, Print PDF in C# and C# extract image from PDF.
Terms of Use | Privacy Policy
Copyright © OnBarcode.com . All rights reserved.