Kategorie

How to override admin view in a plugin

Need to create just only two files.  (Click here to download both sample files)

First one - AdminViewLocationExpander

public class AdminViewLocationExpander : IViewLocationExpander
{
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
{
if (context.Values.TryGetValue("Admin", out _))
{
viewLocations = new[] {
$"/Plugins/Misc.CustomPlugin/Admin/Views/{{1}}/{{0}}.cshtml",
$"/Plugins/Misc.CustomPlugin/Admin/Views/Shared/{{0}}.cshtml",
}
.Concat(viewLocations);
}
return viewLocations;
}

public void PopulateValues(ViewLocationExpanderContext context)
{
//add view context only for admin
if (context.AreaName?.Equals("Admin") ?? false)
context.Values["Admin"] = "Y";

return;
}
}

Second file - AdminViewStartup - to register it

public class AdminViewStartup : IGrandStartup
{
public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
//register admin view expander
services.Configure<RazorViewEngineOptions>(options =>
{
options.ViewLocationExpanders.Add(new AdminViewLocationExpander());
});
}
public void Configure(IApplicationBuilder application)
{

}
public int Order => 102;

}

back to top