How to convert node.js to C# code

mostafa ahmed 21 Reputation points
2023-12-05T11:46:11.3366667+00:00

I have this code in node.js and I need to convert it to C# code This method I have to implement it in my project to be consume from other platform.

But it is coded in Node.js and I am using ASP.NET MVC and I do not have idea in node.js anymore

app.post('/webhook', (req, res) => {

    var response

    console.log(req.headers)
    console.log(req.body)

    const message = `v0:${req.headers['x-zm-request-timestamp']}:${JSON.stringify(req.body)}`

    const signature = `lj5423j54235423`

    if (req.headers['x-zm-signature'] === signature) {

        if (req.body.event === 'endpoint.url_validation') {
            
            response = {
                message: {
                    plainToken: req.body.payload.plainToken,
                    encryptedToken: 'retwertwertewr5432fgds'
                },
                status: 200
            }

            console.log(response.message)

            res.status(response.status)
            res.json(response.message)
        } 
    } 
})



My major problem here how to recieve req & res arguments in method and get data from header & body I try this but not works:

public ActionResult Hooksd(HttpRequest req, RestResponse res)
{
   ... 
}
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,605 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,405 questions
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
936 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,611 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 61,026 Reputation points
    2023-12-05T17:05:25.65+00:00

    in MVC you don't pass the request and response objects as parameters, they are properties:

    [HttpPost]
    public ActionResult Hooksd()
    {
       var req = Request;
       var res = Response;
       var signature = "lj5423j54235423";
    
       // convert body to dynamic object
       req.InputStream.Seek(0, System.IO.SeekOrigin.Begin);
       var json = new StreamReader(req.InputStream).ReadToEnd();
       dynamic body = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
    
       req.Headers["x-zm-request-timestamp"] == signature)
       {
            return Json(new {
                message = new {
                    plainToken =  body.payload.plainToken,
                    encryptedToken = "retwertwertewr5432fgds"
                }
            });
       }
               
       ... 
    }
    

    as C# is a typed language, you either have to define a class definition for the posted json, or use a dynamics json library

    0 comments No comments