Skip to content

Instantly share code, notes, and snippets.

@jstemerdink
Last active August 17, 2016 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jstemerdink/6858c5a0c68aa8666b7be7272f2935d1 to your computer and use it in GitHub Desktop.
Save jstemerdink/6858c5a0c68aa8666b7be7272f2935d1 to your computer and use it in GitHub Desktop.

Allow the new EPiServer Forms in your WebForms solution

Read the blog here

Powered by ReSharper image

public class DummyController : Controller
{
public ActionResult Index()
{
// Create an empty Razor view called PartialRender.cshtml in views\shared
return this.PartialView("PartialRender");
}
}
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="FormContainerBlockControl.ascx.cs" Inherits="EPiServerWebForms.Views.Blocks.FormContainerBlockControl" %>
<%@ Import Namespace="EPiServerWebForms.Business.MvcHelpers" %>
<% MvcUtility.RenderPartial("FormsContentArea", this.FakeArea); %>
public partial class FormContainerBlockControl : BlockControlBase<FormContainerBlock>
{
protected ContentArea FakeArea { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
ContentArea contentArea = new ContentArea();
contentArea.Items.Add(new ContentAreaItem
{
ContentLink = this.CurrentBlock.Content.ContentLink
});
this.FakeArea = contentArea;
}
}
[ContentType(DisplayName = "FormsBlock", GUID = "5abfb24d-5fba-4637-87c5-4003f9f54536", Description = "")]
public class FormsBlock : SiteBlockData
{
[Display(
GroupName = SystemTabNames.Content,
Order = 320)]
[CultureSpecific]
[AllowedTypes(typeof(FormContainerBlock))]
public virtual ContentArea FormContentArea { get; set; }
}
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="FormsBlockControl.ascx.cs" Inherits="EPiServerWebForms.Views.Blocks.FormsBlockControl" %>
<%@ Import Namespace="EPiServerWebForms.Business.MvcHelpers" %>
<% MvcUtility.RenderPartial("FormsContentArea", this.CurrentBlock.FormContentArea); %>
@using EPiServer.Core
@model ContentArea
@Html.DisplayForModel()
public static class MvcUtility
{
private static readonly ILogger Log = LogManager.GetLogger();
// Render a partial view
public static void RenderPartial(string partialViewName, object model)
{
if (model == null)
{
return;
}
try
{
HttpContextBase httpContextBase = new HttpContextWrapper(HttpContext.Current);
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Dummy");
ControllerContext controllerContext =
new ControllerContext(new RequestContext(httpContextBase, routeData), new DummyController());
IView view = FindPartialView(controllerContext, partialViewName);
ViewContext viewContext = new ViewContext(
controllerContext,
view,
new ViewDataDictionary { Model = model },
new TempDataDictionary(),
httpContextBase.Response.Output);
view.Render(viewContext, httpContextBase.Response.Output);
}
catch (InvalidOperationException invalidOperationException)
{
Log.Critical("Cannot render partial view for: {0} \r\n {1}", partialViewName, invalidOperationException);
}
catch (NotImplementedException notImplementedException)
{
Log.Critical("Cannot render partial view for: {0} \r\n {1}", partialViewName, notImplementedException);
}
catch (ArgumentNullException argumentNullException)
{
Log.Critical("Cannot render partial view for: {0} \r\n {1}", partialViewName, argumentNullException);
}
}
// Find the view, if not throw an exception
private static IView FindPartialView(ControllerContext controllerContext, string partialViewName)
{
ViewEngineResult result = ViewEngines.Engines.FindPartialView(controllerContext, partialViewName);
if (result.View != null)
{
return result.View;
}
StringBuilder locationsText = new StringBuilder();
foreach (string location in result.SearchedLocations)
{
locationsText.AppendLine();
locationsText.Append(location);
}
throw new InvalidOperationException(
string.Format("Partial view {0} not found. Locations Searched: {1}", partialViewName, locationsText));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment