We can consider Layouts are like as Master Pages in Asp.Net Web Forms. Layouts are help us to maintain consistent look and feel across all the views within your Asp.Net MVC application, like Master Pages.
By default In Asp.Net MVC, at application level we have one _ViewStart file with in Views folder (if you are not using "Area" in your application), allow us to define default Layout page for your Asp.Net MVC application. Here will explore simple ways to apply layout pages for your application by _ViewStart file ---------------------------------------------------------------------------------------------------------------------------
@{
var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["Controller"].ToString();
string cLayout = "";
if (controller == "TimeTracker") {
cLayout = "~/Views/Shared/_LayoutTimeTracker.cshtml";
} else {
cLayout = "~/Views/Shared/_Layout.cshtml";
}
Layout = cLayout;
}
-------------------------------------------------------------------------------------------------------------
As Because at the time of every page rendering every request pass through _ViewStart file. So, if you write this pies of code, then according to controller name layout page will be loaded dynamically.
- Note : Even If you want to use Area in your application, then in every area one _ViewStart file will be there and you have to modify your code accordingly. Will update this Article if i found better solution.
0 comments :
Post a Comment