Here is a simple solution to handle localized routes via Action filters in Nop Commerce 4.0.
Class: CustomActionFilter.cs
public class CustomActionFilter : IActionFilter { public void OnActionExecuting(ActionExecutingContext context) { //Check Action and controller of the route and use it to redirect to your custom action or do any customization as per your requirement. var actionName = ((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor).ActionName; var controllerName = ((Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor).ControllerName; if (!string.IsNullOrEmpty(controllerName) && !string.IsNullOrEmpty(actionName)) { switch (controllerName) { case "Catalog": context.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "TestCategory", action = "Category", categoryId =context.ActionArguments["categoryId"], command=context.ActionArguments["command"] })); break;
case "Product": //your customization break; } } }
public void OnActionExecuted(ActionExecutedContext context) { //No implementation here. Do it as per your requirement }
}
=========================================
Custom Startup
public class StartupTest : INopStartup { public void ConfigureServices(IServiceCollection services, IConfigurationRoot configuration) {
services.AddMvc(options => { //an instant options.Filters.Add(new CustomActionFilter()); //By the type options.Filters.Add(typeof(CustomActionFilter)); }); }
public void Configure(IApplicationBuilder application) { }
public int Order { get { return int.MaxValue; } } }
This may not be an appropriate solution. But can help if you want to handle generic localized routes.
I will try to find a better solution and will share with you guys.
You can try with the Action Filter solution as I mentioned above. Using action filters you can check the Controller and action and can perform you customization.