Reimplementing action filters to asp.net core
This commit is contained in:
17
src/Umbraco.Web.BackOffice/ActionExecutedEventArgs.cs
Normal file
17
src/Umbraco.Web.BackOffice/ActionExecutedEventArgs.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Umbraco.Web.BackOffice
|
||||
{
|
||||
public class ActionExecutedEventArgs : EventArgs
|
||||
{
|
||||
public Controller Controller { get; set; }
|
||||
public object Model { get; set; }
|
||||
|
||||
public ActionExecutedEventArgs(Controller controller, object model)
|
||||
{
|
||||
Controller = controller;
|
||||
Model = model;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.Net.Http.Headers;
|
||||
|
||||
namespace Umbraco.Web.BackOffice.Filters
|
||||
{
|
||||
/// <summary>
|
||||
/// Ensures that the request is not cached by the browser
|
||||
/// </summary>
|
||||
public class DisableBrowserCacheAttribute : ActionFilterAttribute
|
||||
{
|
||||
public override void OnResultExecuting(ResultExecutingContext context)
|
||||
{
|
||||
base.OnResultExecuting(context);
|
||||
|
||||
if (context.HttpContext.Response.StatusCode != 200) return;
|
||||
|
||||
context.HttpContext.Response.GetTypedHeaders().CacheControl =
|
||||
new CacheControlHeaderValue()
|
||||
{
|
||||
NoCache = true,
|
||||
MaxAge = TimeSpan.Zero,
|
||||
MustRevalidate = true,
|
||||
NoStore = true
|
||||
};
|
||||
|
||||
context.HttpContext.Response.Headers[HeaderNames.LastModified] = DateTime.Now.ToString("R"); // Format RFC1123
|
||||
context.HttpContext.Response.Headers[HeaderNames.Pragma] = "no-cache";
|
||||
context.HttpContext.Response.Headers[HeaderNames.Expires] = new DateTime(1990, 1, 1, 0, 0, 0).ToString("R");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using System;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
|
||||
namespace Umbraco.Web.BackOffice.Filters
|
||||
{
|
||||
public class PreRenderViewActionFilterAttribute : ActionFilterAttribute
|
||||
{
|
||||
public override void OnActionExecuted(ActionExecutedContext context)
|
||||
{
|
||||
if (!(context.Controller is Controller umbController) || !(context.Result is ViewResult result))
|
||||
{
|
||||
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(context);
|
||||
}
|
||||
|
||||
|
||||
public static event EventHandler<ActionExecutedEventArgs> ActionExecuted;
|
||||
|
||||
private static void OnActionExecuted(ActionExecutedEventArgs e)
|
||||
{
|
||||
var handler = ActionExecuted;
|
||||
handler?.Invoke(null, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using System.Net;
|
||||
using Microsoft.AspNetCore.Diagnostics;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Umbraco.Core.Configuration.UmbracoSettings;
|
||||
|
||||
namespace Umbraco.Web.BackOffice.Filters
|
||||
{
|
||||
/// <summary>
|
||||
/// Forces the response to have a specific http status code
|
||||
/// </summary>
|
||||
public class StatusCodeResultAttribute : ActionFilterAttribute
|
||||
{
|
||||
private readonly HttpStatusCode _statusCode;
|
||||
|
||||
public StatusCodeResultAttribute(HttpStatusCode statusCode)
|
||||
{
|
||||
_statusCode = statusCode;
|
||||
}
|
||||
|
||||
public override void OnActionExecuted(ActionExecutedContext context)
|
||||
{
|
||||
base.OnActionExecuted(context);
|
||||
|
||||
context.HttpContext.Response.StatusCode = (int)_statusCode;
|
||||
|
||||
var disableIisCustomErrors = context.HttpContext.RequestServices.GetService<IWebRoutingSettings>().TrySkipIisCustomErrors;
|
||||
var statusCodePagesFeature = context.HttpContext.Features.Get<IStatusCodePagesFeature>();
|
||||
|
||||
if (statusCodePagesFeature != null)
|
||||
{
|
||||
// if IIS Custom Errors are disabled, we won't enable the Status Code Pages
|
||||
statusCodePagesFeature.Enabled = !disableIisCustomErrors;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user