I'd like to set a default Razor layout via code in either a base controller or an attribute. It was mentioned in the Docs that this is possible, but I can't figure out how it's done.
I know there's the masterPage parameter to the View method available, but I would like all views returned by a controller to have this value set automatically.
And no, I can't use _ViewStart for this since my views are going to be in various places (this is not a normal MVC site configuration).
Thanks
I think you could just write an ActionFilter like...
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.
The easiest way I can think of doing this is by having your controllers derive from a custom base class that overrides the View method:
In the code above you could explicitly set the masterName to some hardcoded value. Or your Controllers could override the method to provide a Controller-specific layout. Or you could read it from some attribute on the Controller, something similiar to:
Of course you'd have to create your
MyCustomAttribute
.