I think you could just write an ActionFilter like...
public class YourCustomLayoutAttribute : ActionFilter, IResultFilter
{
public void OnResultExecuting(ActionExecutedContext
filterContext)
{
var viewResult = filterContext.Result as ViewResult;
if(viewResult != null)
{
// switch the layout
// I assume Razor will follow convention and take the "MasterName" property and change the layout based on that.
viewResult.MasterName = "CustomLayout";
}
}
}
I just wrote this code by the seat of my pants with no compiler so it probably won't compile but you probably get the idea. I think IResultFilter is the correct interface you want, it has methods that execute right before the view is rendered. If this is correct, you should be able to modify the MasterName for the view that is about to be rendered on the fly.
This would be the controller code usage.
[YourCustomLayout] // this should trigger your custom action result for all actions
public class MyController : Controller
{
public ActionResult Index()
{
return View("Index", "MainLayout"); // even if you were to use the overload to set a master, the action result should override it as it executes later in the pipeline.
}
}