Skip to content

Instantly share code, notes, and snippets.

@bmcdavid
Created December 2, 2016 18:37
Show Gist options
  • Save bmcdavid/7d1415464b1d71e1496de03e24c41d6f to your computer and use it in GitHub Desktop.
Save bmcdavid/7d1415464b1d71e1496de03e24c41d6f to your computer and use it in GitHub Desktop.
Example of Episerver Forms 3.0 and 4.0 Webhook handling.
using EPiServer.Framework;
using EPiServer.Framework.Initialization;
using System.Web.Http;
namespace AlloyTen.Business.Initialization
{
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))]
public class WebApiRouting : IInitializableModule
{
public void Initialize(InitializationEngine context)
{
// Pass a delegate to the Configure method.
System.Web.Http.GlobalConfiguration.Configure(Register);
}
public void Uninitialize(InitializationEngine context)
{
}
public static void Register(HttpConfiguration config)
{
//Force JSON responses on all requests
config.Formatters.Clear();
config.Formatters.Add(new System.Net.Http.Formatting.JsonMediaTypeFormatter());
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
// Attribute routing.
config.MapHttpAttributeRoutes();
// Convention-based routing.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
using EPiServer.Logging.Compatibility;
using Newtonsoft.Json.Linq;
using System;
using System.Web.Http;
namespace AlloyTen.Controllers.WebApi
{
[RoutePrefix("api/webhook")]
public class WebhookDataController : ApiController
{
private static readonly ILog _Logger = LogManager.GetLogger(typeof(WebhookDataController));
[Route("contactus")]
[HttpPost]
public string ContactUs([FromBody]JToken jsonbody)
{
try
{
string name = jsonbody["Name"]?.ToString();
string comment = jsonbody["Comment"]?.ToString();
string submitDate = jsonbody["SYSTEMCOLUMN_SubmitTime"]?.ToString();
_Logger.Debug($"Contact us form submitted with name = {name}, comments = {comment} on {submitDate}!");
}
catch (Exception e)
{
_Logger.Error("Failed to process contact us!", e);
}
return string.Empty;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment