我发现了一些值得一提的事情。正如公认的答案所指出的那样,v4.0中没有通信发生了一些重大的变化,尤其是对.NET核心的变化,这改变了很多事情,特别是对于CustomViewEngines,它是首选的覆盖视图的方法。
现在.NET核心上运行的是另一种方法,
IViewLocationExpander(位置扩展程序)
.
例如,要覆盖首先呈现的_adminlayout.cshtml,需要一个viewLocationExpander类:
视图位置扩展程序
public class ViewLocationExpander : IViewLocationExpander
{
public void PopulateValues(ViewLocationExpanderContext context)
{
}
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
{
// Since we are wanting to override the Admin template just look for Admin in the Context, you can also get the controller and view names here.
if (context.AreaName == AreaNames.Admin)
{
//Add the location we want to use instead
viewLocations = new string[] { $"/Plugins/YourPlugin/Views/Admin/{{1}}/{{0}}.cshtml",
$"/Plugins/YourPlugin/Views/Admin/Shared/{{0}}.cshtml"}.Concat(viewLocations);
}
return viewLocations;
}
}
然后需要一个启动文件来告诉nocommerence使用这个新的viewlocationexpander。nocommerence引擎查找所有实现inopstartup的类,并在启动时执行它们。
您的plugintartup.cs
public class StoreManagerStartup : INopStartup
{
public void ConfigureServices(IServiceCollection services, IConfigurationRoot configuration)
{
services.Configure<RazorViewEngineOptions>(options =>
{
options.ViewLocationExpanders.Add(new ViewLocationExpander());
});
}
public void Configure(IApplicationBuilder application)
{
}
public int Order {
get { return 0; } //Return 0 to force this to execute first, otherwise set higher i.e. 1001
}
}
_ adminlayout.cshtml(仅限标题,代码已修改)
@inject IWebHelper webHelper
@inject IWorkContext workContext
@inject IDateTimeHelper dateTimeHelper
@inject IPermissionService permissionService
@inject IEventPublisher eventPublisher
@inject IHttpContextAccessor httpContextAccessor
@inject CommonSettings commonSettings
@inject LocalizationSettings localizationSettings
@inject StoreInformationSettings storeInformationSettings
@using System.Globalization;
@using System.Text.Encodings.Web;
@using Microsoft.AspNetCore.Http;
@using Nop.Core.Domain.Customers;
@using Nop.Web.Framework;
@using Nop.Web.Framework.Events;
@using Nop.Web.Framework.UI;
@using Nop.Core;
@using Nop.Core.Domain;
@using Nop.Core.Domain.Common;
@using Nop.Core.Domain.Localization;
@using Nop.Services.Common;
@using Nop.Services.Customers;
@using Nop.Services.Events;
@using Nop.Services.Helpers;
@using Nop.Services.Security;
如果只想添加到管理菜单,只需从
IAdminmenuPlugin(IAdminmenuPlugin)
public class YourPlugin : BasePlugin, IAdminMenuPlugin
{
private readonly ISettingService _settingService;
private readonly IWebHelper _webHelper;
/**
* Constructor
**/
public YourPlugin(ISettingService settingService, IWebHelper webHelper)
{
this._settingService = settingService;
this._webHelper = webHelper;
}
/**
* Adds the Admin Menu Item
**/
public void ManageSiteMap(SiteMapNode rootNode)
{
var menuItem = new SiteMapNode()
{
SystemName = "Plugins.YourPlugin",
Title = "Your Plugin Title",
ControllerName = "YourPlugin",
ActionName = "Configure",
Visible = true,
RouteValues = new RouteValueDictionary() { { "area", null } },
};
// To add to the root Admin menu use
rootNode.ChildNodes.Insert(1, menuItem); // or rootNode.ChildNodes.Add(menuItem);
/* uncomment to add to the "Plugins" Menu Item
var pluginNode = rootNode.ChildNodes.FirstOrDefault(x => x.SystemName == "Third party plugins");
if (pluginNode != null)
pluginNode.ChildNodes.Add(menuItem);
else
rootNode.ChildNodes.Add(menuItem);
*/
}
}
如果要覆盖索引页,请不要忘记添加任何\u viewstart.cshtml文件,否则将不会加载模板。