如何在ASP.Net MVC中初始化Webhook接收器 [英] How do I initialize a webhook receiver in ASP.Net MVC

查看:211
本文介绍了如何在ASP.Net MVC中初始化Webhook接收器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在此处进行操作并在ASP.Net MVC中使用webhooks,但该指南似乎适用于wep api类型的项目.与API项目不同,我正在使用MVC类型的项目,并且没有Register方法.在MVC中,我们有一个RegisterRoutes方法.那么我该如何在MVC中注册我的webhooks?

I'm following this guide here for installing and using webhooks in ASP.Net MVC, but it looks like this guide is for a wep api type project. I'm using a MVC type project and there is no Register method, unlike a API project. In MVC we have a RegisterRoutes method. So how do I register my webhooks in MVC?

使用注册和配置的API项目,我的ASP.Net MVC站点没有此项目.

an API project that uses Register and config, my ASP.Net MVC site doesn't have this.

MVC使用RegisterRoutes

MVC uses RegisterRoutes

更新日期 在这里,我添加了一个Web api控制器,下面发布的代码就是该Web api控制器中的内容.我将用于webhooks的nuget软件包,global.asax.cs中的注册以及register方法包括在内.但是我在到达代码"ExecuteAsync"时仍然遇到问题,没有遇到断点

UPdate Here I added a web api controller and the code I posted below is whats in the web api controller. I included the nuget packages for webhooks, the registration in global.asax.cs and in the register method. But I'm still having problems reaching the code 'ExecuteAsync' no breakpoints are being hit

public class StripeWebHookHandler : WebHookHandler
{
    // ngrok http -host-header="localhost:[port]" [port]
    // http(s)://<yourhost>/api/webhooks/incoming/stripe/   strip receiver

    public StripeWebHookHandler()
    {
        this.Receiver = StripeWebHookReceiver.ReceiverName;
    }

    public override Task ExecuteAsync(string generator, WebHookHandlerContext context)
    {
        // For more information about Stripe WebHook payloads, please see 
        // 'https://stripe.com/docs/webhooks'
        StripeEvent entry = context.GetDataOrDefault<StripeEvent>();

        // We can trace to see what is going on.
        Trace.WriteLine(entry.ToString());

        // Switch over the event types if you want to
        switch (entry.EventType)
        {
            default:
                // Information can be returned in a plain text response
                context.Response = context.Request.CreateResponse();
                context.Response.Content = new StringContent(string.Format("Hello {0} event!", entry.EventType));
                break;
        }

        return Task.FromResult(true);
    }
}

推荐答案

您可以在MVC项目中混合使用Web api控制器.添加Web api控制器时,您的App_Start中将有一个WebApiConfig.cs文件,您将在其中定义Web api的路由.在该项目中将必要的nuget包添加到项目后,您可以在其中调用InitializeReceiveStripeWebHooks方法.

You can mix web api controllers in your MVC project. When you add web api controllers, you will have a WebApiConfig.cs file in your App_Start where you will definie the routing for web api. That is where you can call the InitializeReceiveStripeWebHooks method after adding the necessary nuget packages to your project.

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        // Load receivers
        config.InitializeReceiveStripeWebHooks();
    }
}

创建新项目时,Visual Studio为您提供不同的项目模板.您可以选择一个包含MVC和Web Api支持的WebApi.但是,如果您的项目是使用MVC模板创建的,则默认情况下将不支持Web api.在这种情况下,您可以按照以下步骤手动添加它.

When you create a new project, Visual studio present you with different project templates. You can select the WebApi one which includes MVC and Web Api support. But if your project is created using the MVC template, it will not have the web api support by default. In that case, you can manually add it by following these steps.

第1步

在App_Start文件夹中创建一个名为WebApiConfig.cs的新类.在该课程中具有以下内容

Create a new class called WebApiConfig.cs in the App_Start folder. Have the below content in that class

using System.Web.Http;
public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
    }
}

第2步

现在转到global.asax并更新Application_Start事件,以在我们新创建的WebApiConfig类中包括对Register方法的调用.

Now go to global.asax and update the Application_Start event to include a call to the Register method in our newly created WebApiConfig class.

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    GlobalConfiguration.Configure(WebApiConfig.Register);  // This is the new line

    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

现在,您可以右键单击项目并添加新的WebApiController类,并且Web api应该可以正常工作了.

Now you can right click on project and add new WebApiController classes and web api's should work now.

这篇关于如何在ASP.Net MVC中初始化Webhook接收器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆