【问题标题】:Should I change @Html.Partial to @Html.PartialAsync as Visual Studio suggest?我应该按照 Visual Studio 的建议将 @Html.Partial 更改为 @Html.PartialAsync 吗?
【发布时间】:2018-06-09 13:19:17
【问题描述】:

在我的代码中,我有 @Html.Partial("_StatusMessage", Model.StatusMessage),但 Visual Studio 警告我:Error MVC1000: Use of IHtmlHelper.Partial may result in application deadlocks. Consider using <partial> Tag Helper or IHtmlHelper.PartialAsync.

我应该禁用这个错误还是真的应该将@Html.Partial 更改为@Html.PartialAsync,为什么?

【问题讨论】:

标签: c# razor .net-core


【解决方案1】:

是的,我们应该, 请参阅他们官方网站的以下部分

从 HTML 帮助程序迁移

考虑以下异步 HTML Helper 示例。产品的集合被迭代和显示。根据 PartialAsync 方法的第一个参数,加载 _ProductPartial.cshtml 部分视图。 Product 模型的一个实例被传递给分部视图进行绑定。

CSHTML 
    @foreach (var product in Model.Products)
    {
        @await Html.PartialAsync("_ProductPartial", product)
    }

以下 Partial Tag Helper 实现了与 PartialAsync HTML Helper 相同的异步呈现行为。模型属性被分配了一个产品模型实例,用于绑定到局部视图。

CSHTML 
@foreach (var product in Model.Products)
{
    <partial name="_ProductPartial" model="product" />
} 

复制自 https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/partial-tag-helper?view=aspnetcore-2.1

【讨论】:

    【解决方案2】:

    @niico,回应您对

    的评论

    &lt;partial&gt; 标签助手在哪里适合这个?

    根据我从文档和 github 中找到的信息,您应该使用 @Html.PartialAsync()&lt;partial name="_Post" /&gt; 代替 @Html.Partial()。但是 &lt;partial name="" /&gt; 元素似乎不适用于我的 .NET CORE 版本从今天 (8/23/18) 开始更新

    请看:

    https://docs.microsoft.com/en-us/aspnet/core/mvc/views/tag-helpers/built-in/partial-tag-helper?view=aspnetcore-2.1

    https://docs.microsoft.com/en-us/aspnet/core/mvc/views/partial?view=aspnetcore-2.1

    https://github.com/IdentityServer/IdentityServer4/pull/2344

    【讨论】:

    • 我遇到了同样的问题,&lt;partial&gt; 不起作用,直到我发现 this comment You're also correct that the Tag Helper has to be registered in the Views/_ViewImports.cshtml file: @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
    【解决方案3】:

    从 ASP.NET Core 2.1 开始...使用 @await Html.PartialAsync() 而不是 @Html.Partial()

    【讨论】:

    • 问题询问是否应该使用 PartialAsync,如果是,为什么。只是说使用 Html.PartialAsync 而不是 Html.Partial() 并没有帮助
    【解决方案4】:

    我不知道你的代码,但我认为这个应该回答你的问题: When to use @await Html.PartialAsync in a View in MVC 6

    根据关于部分视图的 ASP.NET MVC 文档。 https://docs.asp.net/en/latest/mvc/views/partial.html

    The PartialAsync method is available for partial views containing asynchronous code (although code in views is generally discouraged):
    

    还有页面上的注释。

    If your views need to execute code, the recommended pattern is to use a view component instead of a partial view.
    

    因此,您应该使用 Partial 并避免使用 PartialAsync,如果您发现自己使用 PartialAsync,您应该质疑自己是否做错了什么,也许您应该改用 ViewComponent 或将逻辑从视图移动到控制器.

    【讨论】:

    • 标签助手在哪里适合这个?
    【解决方案5】:

    您可以在 MVC 项目中为“Warning MVC1000 Use of IHtmlHelper.Partial”尝试此方法。

    Source link

    <partial name="~/Views/Folder/_PartialName.cshtml" />
    <partial name="/Views/Folder/_PartialName.cshtml" />

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-11
      • 2013-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多