T
T
tmtm6532019-07-02 10:10:38
ASP.NET
tmtm653, 2019-07-02 10:10:38

How to make a "Webhook"?

I need the application "WinForm" for correspondence in viber.
"Webhook" is planned to receive data (events) from viber, then the received data will be used in the "WinForm" application.
I did:

  1. created the project "ASP.NET Web Application (.NET Framework)";
  2. selected template - "Empty" + "MVC" + "API";
  3. added controller "Controller MVC 5 - empty". Controller name "HookController";
  4. I start the application "Postman";
  5. Postman. set request "POST" ;
  6. Postman. I set the link " localhost:44836/Hook ";
  7. Postman. I press "SEND";
  8. See the picture for the result "-= RESULT =-";

If I understood the theory correctly, then after performing the "Postman" action. I press "SEND", the ViberProcess(HttpContext context) method should be executed in the HookController.cs controller and the code should stop at the breakpoint.
This is not happening.
Viber REST API documentation - link
Question.
How to make "Webhook" correctly?
HookController.cs Code
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

using System.Web;
using System.Web.Mvc;

//
using System.Runtime.Remoting.Contexts;

namespace WebAppl1.Controllers
{
    public class HookController : Controller
    {
        // GET: Hook
        //public ActionResult Index()
        //{
        //    return View();
        //}

         [HttpPost]
        // [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public void ViberProcess(HttpContext context)
        {
            try
            {
                Stream s = context.Request.InputStream;
                // Stream s = Context.Request.InputStream;
                // or  Stream s  =  HttpContext.Current.Request.InputStream;
                s.Position = 0;
                StreamReader reader = new StreamReader(s);
                string jsonText = reader.ReadToEnd();

                // Other code that converts json text to classes
            }
            catch (Exception e)
            {
                // .....
            }
        }

    }

}

5d1b02fbbbc03379840098.jpeg
7. "Postman". Click "SEND";
5d1b030c3c16b211008424.png
8. The result, see the picture "- = RESULT = -";
5d1b031992cea605684353.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
Peter, 2019-07-02
@tmtm653

You must have routing defined in your ASP.NET application.
As a rule, by default there is "Controller name"/['index']
where index is substituted as a default value.

app.UseMvc(routes => {
      routes.MapRoute("default",
                    "{controller}/{action}",
                     new { controller = "Home", action = "Index" });
});

It follows that it will send a request to localhost:44836/Hook
looking for the controller HookController and the Index method that is marked with [HttpPost]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question