How do I initialize a webhook receiver in ASP.Net MVC

你。 提交于 2019-12-02 03:59:59

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();
    }
}

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.

Step #1

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 }
        );
    }
}

Step #2

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);
}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!