Dynamic change ViewStart layout path in MVC 3

前端 未结 4 642
不知归路
不知归路 2021-02-10 07:56

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:11

    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?

提交回复
热议问题