ViewLocationExpander cannot find views in plugin if same view exists in theme or default view in nop 4.4

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
2 years ago
Hi

The problem is when I'm using ViewLocationExpander to find the view from the mentioned location, it's not working. Because same view is exists within the theme folder. ViewLocationExpander works perfectly for those which has no default view within theme folder. So, I' want to override the default path. The default path is mentioned within ThemeableViewLocationExpander (Nop.Web.Framework.Themes).
My ViewLocationExpander:
 public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
        {
            if (context.Values.TryGetValue(THEME_KEY, out string theme))
            {
                viewLocations = new string[] { $"/Themes/{theme}/Views/{Constants.PluginName}/Shared/"+splitcomp+".cshtml"
                }.Concat(viewLocations);
            }

            return viewLocations;
        }
    }

Startup.cs:
 public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
        {
            services.Configure<RazorViewEngineOptions>(options =>
            {
                options.ViewLocationExpanders.Add(new PluginViewExpander());
            });    
        }


How to override this path? am I missing somthing?

Thanks in Advance.
2 years ago
The system will search the dirctories in order and use the first View it finds
So you can have a number of search directories defined for a controller and/or Viewname
.....
if (context.ControllerName == "YourController" && context.ViewName != "")
{
    var newViewLocations = new[]
    {
   $"~/Plugins/{plugin.SystemName}/Views/{context.ControllerName}/{context.ViewName}.cshtml",
        $"~/Themes/" + StoreTheme + "/Views/Shared/" + context.ViewName + ".cshtml",
        $"~/Themes/" + StoreTheme + "/Views/Product/" + context.ViewName + ".cshtml",
        $"~/Views/Shared/" + context.ViewName + ".cshtml",
        $"~/Views/Product/" + context.ViewName + ".cshtml",
    };
}
viewLocations = viewLocations.Concat(newViewLocations);
....
or You can define rules for specific Partial Views
i.e.
if (context.ControllerName == "YourController" && context.ViewName == "_AddToCart")
{
    viewLocations = new[] { $"/Plugins/Plugin.Name/Views/FrontView/_AddToCart.cshtml"\}.Concat(viewLocations);
}
Here the exisiting viewLocations are concatenated on the end of the list and the plugin one is added at the start so it is found first

List all the different rule combinations one after another in order of search to find the corret view
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.