VintaSoft Barcode .NET SDK v11.0
In This Topic
    Get barcode image in ASP.NET MVC
    In This Topic

    All resource-intensive operations in ASP.NET MVC application are performed asynchronously, so the barcode generation should be also performed asynchronously.

    1. Asynchronous barcode generation

    The process of asynchronous barcode generation can be divided into the following steps:
    1. Client-side: ASP.NET MVC application sends an asynchronous request to a web service
    2. Server-side: The web service generates a barcode and sends the results to the application
    3. Client-side: The application receives response from the web service and processes the generated barcode

    Client-side of ASP.NET MVC application

    The WebBarcodeWriterJS class, from Vintasoft.Barcode.js file, can be used on a client-side of ASP.NET MVC application for generating of barcode image.
    The WebBarcodeWriterJS class allows to:
    Important: JQuery (version 1.11 or higher) and Vintasoft.Shared.js file are necessary for correct work of Vintasoft.Barcode.js file.

    Example: Here is JavaScript example that demonstrates how to generate an image of QR Code barcode:
    // create service that allows to write barcode
    var barcodeService = new Vintasoft.Shared.WebServiceControllerJS("/vintasoft/api/Barcode/");
    
    // create the barcode writer
    var barcodeWriter = new Vintasoft.Barcode.WebBarcodeWriterJS(barcodeService);
    // specify that QR barcode must be generated
    barcodeWriter.get_Settings().set_BarcodeType(new Vintasoft.Barcode.Web2DBarcodeTypeEnumJS("QR"));
    
    // send an asynchronous request for getting barcode image as Base64 string
    barcodeWriter.getBarcodeAsBase64Image(__writeBarcode_success, __writeBarcode_failed);
    
    
    // Barcode image is generated successfully.
    function __writeBarcode_success(data) {
        if (data.success) {
            var imageAsBase64string = data.barcode;
            document.getElementById("barcodeImage").src = imageAsBase64string;
        }
        else {
            alert(data.errorMessage);
        }
    }
    
    // Barcode generation is failed.
    function __writeBarcode_failed(data) {
        // show information about error
        alert(data.responseJSON.ExceptionMessage);
    }
            
    


    Server-side of ASP.NET MVC application

    Depending on application architecture the web service, which generates barcodes, can be based on Web 2 API controller (ASP.NET MVC 5) or Web API controller (ASP.NET MVC 4).
    SDK provides the following ready-to-use web services:
    Important: Vintasoft.Barcode.dll, Vintasoft.Shared.dll and Vintasoft.Shared.Web.dll assemblies are necessary for correct work of Vintasoft.Barcode.Web.ApiControllers.dll, Vintasoft.Barcode.Web.WcfServices.dll or Vintasoft.Barcode.Web.HttpHandlers.dll assembly.

    Here is a code snippet that demonstrates how in ASP.NET MVC 4 application create Web API controller:
    using Vintasoft.Barcode.Web.ApiControllers;
    
    namespace AspNetMvcBarcodeDemos.Controllers
    {
        // API controller that handles HTTP requests from clients and
        // allows to read barcodes from image generate barcode image.
        public class BarcodeController : VintasoftBarcodeApiController
        {
    
            public BarcodeController()
                : base()
            {
            }
    
        }
    }
                    
    

    Here is a code snippet that demonstrates how in ASP.NET MVC 4 application register route to the created Web API controller:
    using System.Web.Http;
    
    namespace AspNetMvcBarcodeDemos
    {
        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                config.Routes.MapHttpRoute(
                    name: "DemoAPI",
                    routeTemplate: "vintasoft/api/{controller}/{action}"
                );
            }
        }
    }
                    
    


    2. Demo applications

    The AspNetMvc5BarcodeDemos project (can be found in distributive package of VintaSoft Barcode .NET SDK) contains the BarcodeWriterDemo, which demonstrates how to implement an asynchronous generation of barcode image using the web service based on Web API 2 controller.
    The AspNetMvc4BarcodeDemos project (can be found in distributive package of VintaSoft Barcode .NET SDK) contains the BarcodeWriterDemo, which demonstrates how to implement an asynchronous generation of barcode image using the web service based on Web API controller.