Dynamic change ViewStart layout path in MVC 3

前端 未结 4 2236
囚心锁ツ
囚心锁ツ 2021-02-10 07:42

In my MVC project has 2 Areas which is Admin and Client and I need to dynamic config Layout for Client side, In _ViewStart (in client) file will set layout for all of client pag

4条回答
  •  孤独总比滥情好
    2021-02-10 08:15

    Remember that anything within the @{ ... } is treated as code. So, it should be a simple matter of placing a condition in there to change how it's inherited:

    @{
      Layout = "~/Views/Shared/_Layout.cshtml";
      if (User.Current.IsAuthenticated) {
        Layout = "~/Views/Shared/_AdminLayout.cshtml";
      }
    }
    

    Though you're probaby better off looking at Themes (and have an admin/user theme). Alternatively, you can make your _Layout.cshtml smarter and have it handle the different views based on conditions as well.

    See Also: MVC3 Razor - Is there a way to change the Layout depending on browser request?

提交回复
热议问题