Fixes: U4-5065 Add event to modify the model on front-end for IRenderMvcControllers
This commit is contained in:
16
src/Umbraco.Web/Mvc/ActionExecutedEventArgs.cs
Normal file
16
src/Umbraco.Web/Mvc/ActionExecutedEventArgs.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
|
||||
namespace Umbraco.Web.Mvc
|
||||
{
|
||||
public class ActionExecutedEventArgs : EventArgs
|
||||
{
|
||||
public UmbracoController Controller { get; set; }
|
||||
public object Model { get; set; }
|
||||
|
||||
public ActionExecutedEventArgs(UmbracoController controller, object model)
|
||||
{
|
||||
Controller = controller;
|
||||
Model = model;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using System.Web.Http.Filters;
|
||||
using System.Web.Mvc;
|
||||
using System.Windows.Forms;
|
||||
using Umbraco.Web.Models;
|
||||
|
||||
namespace Umbraco.Web.Mvc
|
||||
|
||||
48
src/Umbraco.Web/Mvc/PreRenderViewActionFilterAttribute.cs
Normal file
48
src/Umbraco.Web/Mvc/PreRenderViewActionFilterAttribute.cs
Normal file
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Web.Mvc;
|
||||
|
||||
namespace Umbraco.Web.Mvc
|
||||
{
|
||||
public class PreRenderViewActionFilterAttribute : ActionFilterAttribute
|
||||
{
|
||||
public override void OnActionExecuted(ActionExecutedContext filterContext)
|
||||
{
|
||||
var umbController = filterContext.Controller as UmbracoController;
|
||||
if (umbController == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var result = filterContext.Result as ViewResultBase;
|
||||
if (result == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var model = result.Model;
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var args = new ActionExecutedEventArgs(umbController, model);
|
||||
OnActionExecuted(args);
|
||||
|
||||
if (args.Model != model)
|
||||
{
|
||||
result.ViewData.Model = args.Model;
|
||||
}
|
||||
|
||||
base.OnActionExecuted(filterContext);
|
||||
}
|
||||
|
||||
|
||||
public static event EventHandler<ActionExecutedEventArgs> ActionExecuted;
|
||||
|
||||
private static void OnActionExecuted(ActionExecutedEventArgs e)
|
||||
{
|
||||
EventHandler<ActionExecutedEventArgs> handler = ActionExecuted;
|
||||
if (handler != null) handler(null, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@ namespace Umbraco.Web.Mvc
|
||||
/// <summary>
|
||||
/// A controller to render front-end requests
|
||||
/// </summary>
|
||||
[PreRenderViewActionFilter]
|
||||
public class RenderMvcController : UmbracoController, IRenderMvcController
|
||||
{
|
||||
|
||||
|
||||
@@ -35,7 +35,7 @@ namespace Umbraco.Web.Mvc
|
||||
/// <summary>
|
||||
/// Returns the current UmbracoContext
|
||||
/// </summary>
|
||||
protected UmbracoContext UmbracoContext { get; private set; }
|
||||
public UmbracoContext UmbracoContext { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Returns the current ApplicationContext
|
||||
|
||||
@@ -361,6 +361,8 @@
|
||||
<Compile Include="Models\PartialViewMacroModelExtensions.cs" />
|
||||
<Compile Include="Models\PostRedirectModel.cs" />
|
||||
<Compile Include="Models\PublishedProperty.cs" />
|
||||
<Compile Include="Mvc\ActionExecutedEventArgs.cs" />
|
||||
<Compile Include="Mvc\PreRenderViewActionFilterAttribute.cs" />
|
||||
<Compile Include="Mvc\RedirectToUmbracoUrlResult.cs" />
|
||||
<Compile Include="PropertyEditors\TagsDataController.cs" />
|
||||
<Compile Include="PublishedCache\MemberPublishedContent.cs" />
|
||||
|
||||
Reference in New Issue
Block a user