我愿意从我的插件中覆盖"_RentalInfo.cshtml"和"_AddToCart.cshtml"页面。为此,我创建了自定义视图引擎,并将文件的路径提供给"PartViewLocationFormats,ViewLocationFormats"。自定义视图引擎类
public class CustomViewEngine : ThemeableRazorViewEngine
{
public CustomViewEngine()
{
PartialViewLocationFormats = new[] { "~/Plugins/Misc.Rental/Views/Product/{0}.cshtml" };
ViewLocationFormats = new[] { "~/Plugins/Misc.Rental/Views/Product/{0}.cshtml" };
}
}
然后,我在插件中创建了 RouteProvider.cs 文件,并在视图引擎列表中添加了我的"CustomViewEngine"。我还将路由提供商的优先级设置为最高。
//RouteProvider
public partial class RouteProvider : IRouteProvider
{
#region Fields
public int Priority
{
get
{
return int.MaxValue;
}
}
#endregion
#region Methods
public void RegisterRoutes(RouteCollection routes)
{
ViewEngines.Engines.Add(new CustomViewEngine());
}
#endregion
}
最后,根据我的"自定义视图引擎"中提到的路径,我在插件中的"视图"文件夹下创建了"_RentalInfo.cshtml"和"_AddToCart.cshtml"页面。
但是,也只是显示默认的NopCommerce默认部分视图。任何人都可以查看我的代码并让我知道,我执行错误或缺少什么以覆盖部分视图。
我在插件中使用以下代码
public PluginRazorViewEngine()
{
PartialViewLocationFormats = new[]
{
"~/Plugins/My.Plugin/Views/{1}/{0}.cshtml",
"~/Plugins/My.Plugin/Views/Shared/{0}.cshtml",
};
ViewLocationFormats = new[]
{
"~/Plugins/My.Plugin/Views/{1}/{0}.cshtml",
"~/Plugins/My.Plugin/Views/Shared/{0}.cshtml",
};
AreaPartialViewLocationFormats = new[]
{
"~/Plugins/My.Plugin/Views/{1}/{0}.cshtml",
"~/Plugins/My.Plugin/Views/Shared/{0}.cshtml",
};
AreaViewLocationFormats = new[]
{
"~/Plugins/My.Plugin/Views/{1}/{0}.cshtml",
"~/Plugins/My.Plugin/Views/Shared/{0}.cshtml",
};
}
// {0} in format gets filled with view name.
// {1} in format gets filled with controller name.
我不确定哪一个可以解决您的问题。
只需将My.Plugin替换为Misc.Rental,您应该没问题。