how to override a views Layout declaration

前端 未结 4 1327
無奈伤痛
無奈伤痛 2021-01-19 15:43

In asp.net MVC 3 is there a way to override the Layout declaration set in a view from a controller or action filter?

@{
    Layout = \"~/Views/Shared/_Layout.cshtm         


        
4条回答
  •  灰色年华
    2021-01-19 16:17

    Another place where you can control the layout is in the _ViewStart.cshtml.

    Here, you can do the logic you need and programatically specify which layout to use. This allows you to place the logic in only one place and keep it out of the view.

    @{
        if(myBusinessRule)
        {
           Layout = "~/Views/Shared/_Layout.cshtml";
        }
        else
        {
           Layout = "~/Views/Shared/_SecondaryLayout.cshtml";
        }
    }
    

    Blog post where it was introduced by Scott Gu

提交回复
热议问题