Enable Orchard to support multiple Layout and Orchard
By default, Orchard has only one Layout. Sometimes, multiple master pages are required for our site. To enable Orchard to support multiple Layout, the following is a solution.
I. Create LayoutFilter
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Orchard;
using Orchard.Mvc.Filters;
namespace MyTheme.Filters
{
public class LayoutFilter : FilterProvider, IResultFilter
{
private readonly IWorkContextAccessor _wca;
public LayoutFilter(IWorkContextAccessor wca)
{
_wca = wca;
}
public void OnResultExecuting(ResultExecutingContext filterContext)
{
WorkContext workContext = _wca.GetContext();
if (HttpContext.Current.Request.Url.PathAndQuery.IndexOf("xxx", StringComparison.OrdinalIgnoreCase) > -1)
{
workContext.Layout.Metadata.Alternates.Add("Layout__empty");
}
}
public void OnResultExecuted(ResultExecutedContext filterContext) { }
}
}
The above implementation code is when our URL address, there are xxx these three characters, let our page call Layout-empty.cshtml this master page.
Ii. Go to the View of the template to create the corresponding Layout page.
Now, for the code above, go to the View folder of our own template to create the Layout-empty.cshtml master page. Note: if we do not create this master page, or Orchard cannot find this master page, Orchard will still find its own default master page to show us the page.
Scan, follow the most courses, and obtain daily exercises