Skip to content

Instantly share code, notes, and snippets.

@martingust martingust/NopStartup.cs
Last active Dec 1, 2017

Embed
What would you like to do?
Custom Action Filters in nopCommerce v4
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Nop.Core.Infrastructure;
namespace Nop.Plugin.Misc.CustomActionFilterDemo.Filters
{
public class NopStartup : INopStartup
{
public void ConfigureServices(IServiceCollection services, IConfigurationRoot configuration)
{
services.Configure<MvcOptions>(config =>
{
config.Filters.Add<ProductDetailsActionFilter>();
});
}
public void Configure(IApplicationBuilder application)
{
}
public int Order => 0;
}
}
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
using Microsoft.AspNetCore.Mvc.Filters;
using Nop.Web.Controllers;
using Nop.Web.Models.Catalog;
namespace Nop.Plugin.Misc.CustomActionFilterDemo.Filters
{
public class ProductDetailsActionFilter : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext context)
{
if (!(context.ActionDescriptor is ControllerActionDescriptor actionDescriptor)) return;
if (actionDescriptor.ControllerTypeInfo != typeof(ProductController) ||
actionDescriptor.ActionName != "ProductDetails" ||
context.HttpContext.Request.Method != "GET")
{
return;
}
var result = context.Result as ViewResult;
if (result == null) return;
var model = result.Model as ProductDetailsModel;
if (model == null) return;
model.Name += " - This is magic from a Custom Action Filter";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.