Procedure 1: Implement Layout Layout Using the _ViewStart File in the Root of the Views Folder
This method is the easiest way for beginners to control the rendering of layouts in your ASP.NET MVC application. We can identify the controller and display the layouts as a parser, to do this, we can write our code in the _ViewStart file in the root directory of the "Views" folder. The following is an example of how this can be done.
@{ var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["Controller"].ToString(); string cLayout = ""; if (controller == "Webmaster") { cLayout = "~/Views/Shared/_WebmasterLayout.cshtml"; } else { cLayout = "~/Views/Shared/_Layout.cshtml"; } Layout = cLayout; }
Procedure 2: set the layout by returning from ActionResult
One of the great features of ASP.NET MVC is that we can override the default layout rendering by returning the layout from ActionResult. Thus, it is also a way to render various layouts in an ASP.NET MVC application. The following code example shows how to do this.
public ActionResult Index() { SampleModel model = new SampleModel(); //Any Logic return View("Index", "_WebmasterLayout", model); }
Procedure 3: View-wise Layout (Defining a Layout in Each View from Above)
ASP.NET MVC provides us with such a wonderful feature and the ability to send faxes to override the default layout rendering by defining the layout on the view. To implement this, we can write our code as follows in each view.
@{ Layout = "~/Views/Shared/_WebmasterLayout.cshtml"; }
Procedure 4: Placing the _ViewStart File in Each of the Directories
This is a very useful way to set different layouts for each controller in your ASP.NET MVC application. If we want to set the default layout for each directory, we can do this by placing the _ViewStart file in each of the directories with the necessary layout information, as shown below:
@{ Layout = "~/Views/Shared/_WebmasterLayout.cshtml"; }
Anand Gaikwad Aug 29 '17 at 5:28 2017-08-29 05:28
source share