Force all Areas to use same Layout

后端 未结 3 1225
野性不改
野性不改 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:47

    Expanding on the answer by Jupaol....

    At least in VS2013, the _ViewStart.cshtml file is added by default when creating the area, so it's already there, and you can change the contents as he notes to point to the root _Layout.cshtml. You can then remove the _Layout.cshtml in the area, since it is no longer used (and a potential source of confusion now)

    However, by so doing any routing performed in that root _Layout.cshtml will need to consider areas.
    The default _Layout.cshtml has a number of ActionLink helpers that need a slight modification:

    Add the RouteValueDictionary param to any ActionLink calls by setting Area="". Note that empty string refers to the root level. This will allow these links to work correctly when invoked from within an area, still work when invoked from the root.

    e.g.:

  • @Html.ActionLink("Home", "Index", "Home", new { Area = "" }, null)
提交回复
热议问题