我遵循这个指南这里在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”时仍然有问题--没有任何断点被击中
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);
}
}发布于 2017-10-26 00:24:42
您可以在MVC项目中混合web控制器。当您添加web控制器时,您将在您的WebApiConfig.cs文件中包含一个App_Start文件,在该文件中您将定义web的路由。这就是在向项目中添加必要的nuget包之后可以调用InitializeReceiveStripeWebHooks方法的地方。
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的新类。在那堂课上有以下内容
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方法的调用。
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现在应该可以工作了。
https://stackoverflow.com/questions/46943556
复制相似问题