Calling Node.js from ASP.NET Core

Published 04-01-2019 05:03 PM 2,046 Views
Microsoft
First published on MSDN on Oct 23, 2017

Here are the steps to invoke Node.js from ASP.NET Core Website

  1. Add below line in the ConfiguraServices() in Startup.cs

    public
    void
    ConfigureServices(IServiceCollection services)


    {


    services.AddMvc();


    services.AddNodeServices();


    }




  2. Add constructor in the Controller

    public
    class
    HomeController : Controller


    {


    INodeServices
    nodeServices;


    public
    HomeController(INodeServices nodeServices)


    {


    this.nodeServices =
    nodeServices;


    }



  3. Call NodeServices as shown below

    public
    async  Task<IActionResult>
    About()


    {


    var
    msg = await
    nodeServices.InvokeAsync<string>("./hello.js",
    1);



    ViewData["Message"] = msg;


    return View();


    }



  4. Add following code to js file

    module.exports = function (callback)


    {


    var
    message = 'Hello
    world';


    callback(null, message);


    };




%3CLINGO-SUB%20id%3D%22lingo-sub-392475%22%20slang%3D%22en-US%22%3ECalling%20Node.js%20from%20ASP.NET%20Core%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-392475%22%20slang%3D%22en-US%22%3E%0A%20%26lt%3Bmeta%20http-equiv%3D%22Content-Type%22%20content%3D%22text%2Fhtml%3B%20charset%3DUTF-8%22%20%2F%26gt%3B%3CSTRONG%3E%20First%20published%20on%20MSDN%20on%20Oct%2023%2C%202017%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3E%3CP%3EHere%20are%20the%20steps%20to%20invoke%20Node.js%20from%20ASP.NET%20Core%20Website%3C%2FP%3E%0A%20%20%3COL%3E%0A%20%20%20%3CLI%3EAdd%20below%20line%20in%20the%20ConfiguraServices()%20in%20Startup.cs%20%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CP%3Epublic%20%3CBR%20%2F%3E%20void%20%3CBR%20%2F%3E%20ConfigureServices(IServiceCollection%20services)%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Eservices.AddMvc()%3B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Eservices.AddNodeServices()%3B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7D%3C%2FP%3E%0A%20%20%20%20%3CP%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%20%20%20%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3EAdd%20constructor%20in%20the%20Controller%3CP%3Epublic%20%3CBR%20%2F%3E%20class%20%3CBR%20%2F%3E%20HomeController%20%3A%20Controller%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EINodeServices%20%3CBR%20%2F%3E%20nodeServices%3B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Epublic%20%3CBR%20%2F%3E%20HomeController(INodeServices%20nodeServices)%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Ethis.nodeServices%20%3D%20%3CBR%20%2F%3E%20nodeServices%3B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7D%3C%2FP%3E%0A%20%20%20%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3ECall%20NodeServices%20as%20shown%20below%3CP%3Epublic%20%3CBR%20%2F%3E%20async%26nbsp%3B%20Task%3CIACTIONRESULT%3E%20%3CBR%20%2F%3E%20About()%3C%2FIACTIONRESULT%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Evar%20%3CBR%20%2F%3E%20msg%20%3D%20await%20%3CBR%20%2F%3E%20nodeServices.InvokeAsync%3CSTRING%3E(%22.%2Fhello.js%22%2C%20%3CBR%20%2F%3E%201)%3B%3C%2FSTRING%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%3C%2FP%3E%3CBR%20%2F%3E%3CP%3EViewData%5B%22Message%22%5D%20%3D%20msg%3B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Ereturn%20View()%3B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7D%3C%2FP%3E%0A%20%20%20%3C%2FLI%3E%3CBR%20%2F%3E%20%3CBR%20%2F%3E%3CLI%3EAdd%20following%20code%20to%20js%20file%3CP%3Emodule.exports%20%3D%20function%20(callback)%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Evar%20%3CBR%20%2F%3E%20message%20%3D%20'Hello%20%3CBR%20%2F%3E%20world'%3B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3Ecallback(null%2C%20message)%3B%3C%2FP%3E%3CBR%20%2F%3E%3CP%3E%7D%3B%3C%2FP%3E%0A%20%20%20%20%3CP%3E%3CBR%20%2F%3E%3C%2FP%3E%3CBR%20%2F%3E%3C%2FLI%3E%3CBR%20%2F%3E%3C%2FOL%3E%0A%20%0A%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-392475%22%20slang%3D%22en-US%22%3EFirst%20published%20on%20MSDN%20on%20Oct%2023%2C%202017%20Here%20are%20the%20steps%20to%20invoke%20Node.%3C%2FLINGO-TEASER%3E
Version history
Last update:
‎Aug 24 2020 12:36 PM
Updated by: