首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在ASP.Net MVC中初始化web钩子接收器

如何在ASP.Net MVC中初始化web钩子接收器
EN

Stack Overflow用户
提问于 2017-10-25 23:28:34
回答 1查看 5.9K关注 0票数 2

我遵循这个指南这里在ASP.Net MVC中安装和使用webhooks,但是看起来这个指南是针对wep类型的项目的。我使用的是MVC类型的项目,与API项目不同的是,没有注册方法。在MVC中,我们有一个RegisterRoutes方法。那么我如何在MVC中注册我的webhooks呢?

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

MVC使用RegisterRoutes

UPdate在这里添加了一个web控制器,下面我发布的代码是web控制器中的代码。我包括了webhooks的nuget包、global.asax.cs中的注册和注册方法。但我在到达代码“ExecuteAsync”时仍然有问题--没有任何断点被击中

代码语言:javascript
运行
复制
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);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2017-10-26 00:24:42

您可以在MVC项目中混合web控制器。当您添加web控制器时,您将在您的WebApiConfig.cs文件中包含一个App_Start文件,在该文件中您将定义web的路由。这就是在向项目中添加必要的nuget包之后可以调用InitializeReceiveStripeWebHooks方法的地方。

代码语言:javascript
运行
复制
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将向您提供不同的项目模板。您可以选择包含MVC和WebApi支持的WebApi。但是,如果您的项目是使用MVC模板创建的,默认情况下它将不支持web。在这种情况下,您可以通过以下步骤手动添加它。

步骤1

WebApiConfig.cs文件夹中创建一个名为App_Start的新类。在那堂课上有以下内容

代码语言:javascript
运行
复制
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方法的调用。

代码语言:javascript
运行
复制
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现在应该可以工作了。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46943556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档