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 8, 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

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.




Create barcodes through URL in C# ASP.NET Core

The following guide will create an ASP.NET Core Empty for Windows, which allows you to create QR Code and barcodes through URL.



Here are the requirements:
  • Visual Studio 2022
  • .NET 6.0 SDK
  • OnBarcode.Barcode.ASPNETCore.dll for Windows, or OnBarcode.Barcode.ASPNETCore.Skia.dll for Linux, MacOS, non-Windows


  • Step 1: Create a new project with project template "ASP.NET Core Empty".

  • Step 2: Choose framework .NET 6.0 (Long Term Support).

  • Step 3: Add project reference: assembly "OnBarcode.Barcode.ASPNETCore.dll" or "OnBarcode.Barcode.ASPNETCore.Skia.dll" to the project.

  • Step 4: For Windows: Install package "System.Drawing.Common" (Version 4.5.0 or later) by NuGet.
    For Linux, MacOS, non-Windows: Install package "SkiaSharp" (Version 2.80.0 or later) by NuGet.

  • Step 5: Replace content in file "Program.cs" by below C# codes
    var builder = WebApplication.CreateBuilder(args);
    builder.Services.AddHttpContextAccessor();
    var app = builder.Build();
    // Linear
    app.MapGet("/", async (IHttpContextAccessor httpContextAccessor) =>
    {
        var context = httpContextAccessor.HttpContext;
        if (context != null)
            await OnBarcode.Barcode.ASPNETCore.LinearWebStream.drawBarcodeAsync(context.Request, context.Response);
    });
    // PDF417
    app.MapGet("/PDF417", async (IHttpContextAccessor httpContextAccessor) =>
    {
        var context = httpContextAccessor.HttpContext;
        if (context != null)
            await OnBarcode.Barcode.ASPNETCore.PDF417WebStream.drawBarcodeAsync(context.Request, context.Response);
    });
    // DataMatrix
    app.MapGet("/DataMatrix", async (IHttpContextAccessor httpContextAccessor) =>
    {
        var context = httpContextAccessor.HttpContext;
        if (context != null)
            await OnBarcode.Barcode.ASPNETCore.DataMatrixWebStream.drawBarcodeAsync(context.Request, context.Response);
    });
    // QRCode
    app.MapGet("/QRCode", async (IHttpContextAccessor httpContextAccessor) =>
    {
        var context = httpContextAccessor.HttpContext;
        if (context != null)
            await OnBarcode.Barcode.ASPNETCore.QRCodeWebStream.drawBarcodeAsync(context.Request, context.Response);
    });
    // MicroPDF417
    app.MapGet("/MicroPDF417", async (IHttpContextAccessor httpContextAccessor) =>
    {
        var context = httpContextAccessor.HttpContext;
        if (context != null)
            await OnBarcode.Barcode.ASPNETCore.MicroPDF417WebStream.drawBarcodeAsync(context.Request, context.Response);
    });
    // MicroQRCode
    app.MapGet("/MicroQRCode", async (IHttpContextAccessor httpContextAccessor) =>
    {
        var context = httpContextAccessor.HttpContext;
        if (context != null)
            await OnBarcode.Barcode.ASPNETCore.MicroQRCodeWebStream.drawBarcodeAsync(context.Request, context.Response);
    });
    app.Run();
    

  • Step 6: Run the demo project (Press Ctrl+F5).

  • Step 7: You can send barcode settings through URL as below.
    Eg. Default is Linear barcode
    https://localhost:7158/?DATA=12345678&X=3
    https://localhost:7158/QRCode?DATA=12345678&X=10
    https://localhost:7158/MicroPDF417?DATA=123&X=2





Conclusion

Now you have learned how to create and display 2d and linear barcodes on ASP.NET Core Razor page, and how to generate QR Code and barcodes through URL using MiniAPI.
















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.