I am creating advanced site that is based in 99% on one view. I have created the following code which is responsible to point all controllers actions which dont have corresponding view to universal view: ~/Views/Shared/UniversalView.cshtml.
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
foreach (IViewEngine engine in ViewEngines.Engines)
{
if (engine is RazorViewEngine)
{
RazorViewEngine razorEngine = (RazorViewEngine)engine;
var locationFormats = razorEngine.ViewLocationFormats;
List<string> l = new List<string>(locationFormats);
l.Add("~/Views/Shared/UniversalView.cshtml");
razorEngine.ViewLocationFormats = l.ToArray();
}
}
}
I am preety sure that there will be much better method to achive that effect. Any ideas?
You can declare a BaseController and all your controller must inherit from it.
Then just add a method in BaseController like
From other controllers you can call this method instead of traditional View().
If you will specify the view name it will take that view else default is Universal view.