Change Layout(Master Page) of view in ASP.NET MVC without recreate it

前端 未结 4 375
星月不相逢
星月不相逢 2021-01-31 08:08

I\'m using ASP.NET MVC 3 with Razor views. When you want to create a view you can choose a layout (master page) for your view, or leave it to choose Default (_Layout).

I

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-31 08:26

    We can change the default rendering of layouts with in _ViewStart file by using the below code:

    @{
     var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["Controller"].ToString();
    
     string layout = "";
     if (controller == "Admin")
     {
     layout = "~/Views/Shared/_AdminLayout.cshtml";
     }
     else
     {
     layout = "~/Views/Shared/_Layout.cshtml";
     }
    
     Layout = layout;
    }
    

提交回复
热议问题