Last active
December 1, 2017 08:57
-
-
Save martingust/d5cc3204ba505ae6472335ff32dbc8eb to your computer and use it in GitHub Desktop.
Custom Action Filters in nopCommerce v4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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