mcselasvegas wrote:Hi Chuck,
thank you for sharing your code... one question - did you override the ExpandViewLocations or did you edit the file itself? /Presentation/Nop.Web.Framework/Themes/ThemeableViewLocationExpander.cs
Hi there, No, I never touch the nop source...I just created my own CustomViewEngine
Basically, my class looks like this;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc.Razor;
using Nop.Web.Framework;
using Nop.Web.Framework.Themes;
namespace Nop.Plugin.Widgets.CorePlugin.Infrastructure
{
public class CustomViewEngine : IViewLocationExpander
{
private const string THEME_KEY = "nop.themename";
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
{
if (context.Values.TryGetValue(THEME_KEY, out string theme))
{
if (context.ViewName == "_CheckoutAttributes")
{
viewLocations = new[] {
$"/Plugins/Widgets.CorePlugin/Themes/"+theme+"/Views/Checkout/_CheckoutAttributes.cshtml",
$"/Plugins/Widgets.CorePlugin/Views/Checkout/_CheckoutAttributes.cshtml"
}
.Concat(viewLocations);
}
else if (context.ViewName == "Components/OrderSummary/Default" | context.ViewName == "Cart" | context.ViewName == "_GiftCardBox")
{
viewLocations = new[] {
$"/Plugins/Widgets.CorePlugin/Views/Shared/{{0}}.cshtml", $"/Views/ShoppingCart/{{0}}.cshtml",
$"/Themes/" + theme + "/Views/ShoppingCart/{{0}}.cshtml"
}
.Concat(viewLocations);
}
// A bunch More else / ifs
}
return viewLocations;
}
public void PopulateValues(ViewLocationExpanderContext context)
{
if (context.AreaName?.Equals(AreaNames.Admin) ?? false)
return;
var themeContext = (IThemeContext)context.ActionContext.HttpContext.RequestServices.GetService(typeof(IThemeContext));
context.Values[THEME_KEY] = themeContext.WorkingThemeName;
}
}
}
In 4.2, you need to initialize your CustomViewEngine from your NopStatup class within your plugin
namespace Nop.Plugin.Widgets.CorePlugin.Infrastructure
{
public class NopStartup : INopStartup
{
public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
services.Configure<RazorViewEngineOptions>(options =>
{
options.ViewLocationExpanders.Add(new CustomViewEngine()); });
//Thread safe fix for async support
services.AddDbContext<CorePluginObjectContext>(optionsBuilder =>
{
optionsBuilder.UseSqlServerWithLazyLoading(services);
}).AddTransient<CorePluginObjectContext>();
}
public void Configure(IApplicationBuilder application)
{
}
public int Order => int.MaxValue;
}
}