Force all Areas to use same Layout

后端 未结 3 1235
野性不改
野性不改 2021-01-30 12:38

I have the following project structure:

  • /Views/Shared/_Layout;

  • /Areas/Area1/Views/ControllerName/Index;

...

  • /Ar
3条回答
  •  臣服心动
    2021-01-30 12:50

    You just have to add a file named:

    _ViewStart.cshtml
    

    Under each area views folder:

    /Areas/Area1/Views/_ViewStart.cshtml
    

    And edit the file to point to the root layout like this:

    @{
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    

    In order for this to work, you do not have to specify a value in the view's layout property, if you do, you would be overriding the global layout

    Note: As Tony mentioned, you could edit each view's layout property to point to the root layout, however this is not the recommended way to do it since you would be coupling your views with your layout and change it would be painful

    Edit 1

    If you would like to use code to set the default view's layout, perhaps you should consider writing a custom view engine.

    Try to google about custom RazorViewEngine and RazorView

    This article could be a good start point

    http://weblogs.asp.net/imranbaloch/archive/2011/06/27/view-engine-with-dynamic-view-location.aspx

    I have not done something like this but I hope I'm pointing you in the right direction

提交回复
热议问题