在区域视图中使用 TagHelpers

Using TagHelpers in Area Views

我花了最后一个小时重构以使用区域,现在我所有的视图似乎都没有功能 taghelpers :/

这就是 Index.cshtml

中的内容
       <div class="btn-group">
           <a asp-controller="Survey" asp-area="Admin" asp-action="Create" class="btn btn-primary">Create New</a>
       </div>

...这是呈现的 HTML :/

<div class="btn-group">
  <a asp-controller="Survey" asp-area="Admin" asp-action="Create" class="btn btn-primary">Create New</a>
</div>

Intellisense 甚至不显示 asp- 前缀,asp- 属性上的语法突出显示也丢失了。

其他 SO 问题参考 "asp-route-area" 但这只是像其他问题一样逐字呈现。

这些在 ~/Views/Name/Index.cshtml 时都工作正常,将它们移到 ~/Areas/Name/Views/Name/ 而 nopers...

有什么想法吗? 史蒂夫

根据官方docs

The @addTagHelper directive makes Tag Helpers available to the view. In this case, the view file is Views/_ViewImports.cshtml, which by default is inherited by all view files in the Views folder and sub-directories; making Tag Helpers available. The code above uses the wildcard syntax (“*”) to specify that all Tag Helpers in the specified assembly (Microsoft.AspNetCore.Mvc.TagHelpers) will be available to every view file in the Views directory or sub-directory.

如果您每个区域使用一个布局,要使用内置标签助手,您应该在 ~/Areas/Name/Views/ 文件夹中添加 _ViewImports.cshtml(如果您使用共享布局则不需要。参见 MusicStore project 用于共享布局示例)。

我猜你每个区域使用了一种布局,但你没有添加 _ViewImports.cshtml ~/Areas/Name/Views/。将 /Views/_ViewImports.cshtml 复制到 ~/Areas/Name/Views/.

事实证明,将 _ViewImports.cshtml 文件添加到 Areas/ 文件夹会将文件的可见性级联到文件夹中的所有 Areas/{area}/views

所以而不是:

-> Areas
--> Area1
----> Views
------> _ViewImports.cshtml
--> Area2
----> Views
------> _ViewImports.cshtml

我们可以这样做:

-> Areas
--> Area1
--> Area2
--> _ViewImports.cshtml

或更直观