How to convert node.js to C# code

mostafa ahmed 41 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)
{
   ... 
}
Microsoft 365 and Office Development Office JavaScript API
Developer technologies .NET Other
Developer technologies ASP.NET Other
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.