Skip to content

Instantly share code, notes, and snippets.

@avisra
Created November 14, 2017 18:24
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 avisra/75d2cac8f9a96300b53ff141a01136a2 to your computer and use it in GitHub Desktop.
Save avisra/75d2cac8f9a96300b53ff141a01136a2 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Net;
using System.Reflection;
using System.Web;
using System.Web.UI;
using Telerik.Sitefinity.Abstractions;
using Telerik.Sitefinity.Mvc;
using Telerik.Sitefinity.Mvc.Proxy;
using Telerik.Sitefinity.Services;
using Telerik.Sitefinity.Web;
using Telerik.Sitefinity.Web.UI.PublicControls;
namespace Avisra.Samples
{
public class MvcControllerProxyNoCache : MvcControllerProxy
{
private bool IsPostBack
{
get { return HttpContext.Current.Request.HttpMethod == WebRequestMethods.Http.Post; }
}
protected override void OnPreRender(EventArgs e)
{
this.ExecuteController();
}
protected override void Render(HtmlTextWriter writer)
{
if (!SystemManager.IsDesignMode && !SystemManager.IsInlineEditingMode)
{
this.Execute();
}
else
{
base.Render(writer);
}
}
public string RenderStatic(Dictionary<string, string> parameters)
{
if (HttpContext.Current.Handler != null)
{
return this.output;
}
var context = HttpContext.Current;
if (context.Request.RequestContext.RouteData.Values.Count == 0)
{
context.Request.RequestContext.RouteData = new SitefinityRoute().GetRouteData(new HttpContextWrapper(context));
}
var controllerActionInvoker = ObjectFactory.Resolve<IControllerActionInvoker>();
var proxyControl = new MvcControllerProxy
{
ControllerName = parameters["ControllerName"],
SerializedSettings = parameters["SerializedSettings"]
};
if (proxyControl.Page == null)
{
var page = HttpContext.Current.Handler as Page;
if (page == null)
{
page = new Page();
page.GetType().InvokeMember("SetIntrinsics", BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic, Type.DefaultBinder, page, new[] { HttpContext.Current });
}
proxyControl.Page = page;
}
string output;
if (controllerActionInvoker.TryInvokeAction(proxyControl, out output))
return output;
return string.Empty;
}
private void Execute()
{
if (this.executed)
return;
var parameters = new Dictionary<string, string>
{
{ "ControllerName", this.ControllerName },
{ "SerializedSettings", this.SerializedSettings }
};
var renderMarkup = new CacheSubstitutionWrapper.RenderMarkupDelegate(RenderStatic);
var wrapper = new CacheSubstitutionWrapper(parameters, renderMarkup);
wrapper.RegisterPostCacheCallBack(HttpContext.Current);
this.executed = true;
}
private void ExecuteController()
{
if (this.controllerExecuted)
return;
var controllerStrategy = ObjectFactory.Resolve<IControllerActionInvoker>();
this.controllerExecuted = controllerStrategy.TryInvokeAction(this, out this.output);
}
private bool controllerExecuted = false;
private bool executed;
private string output;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment