何时在 MVC 6 的视图中使用 @await Html.PartialAsync [英] When to use @await Html.PartialAsync in a View in MVC 6

查看:46
本文介绍了何时在 MVC 6 的视图中使用 @await Html.PartialAsync的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Scott Hanselman 的一篇博客中注意到他在使用 .Net 5 (MVC 6) 时在他的视图中使用了以下代码:

I noticed on one of Scott Hanselman's blogs he uses the following code in his Views when using .Net 5 (MVC 6):

@await Html.PartialAsync("_LoginPartial")

对比

@Html.Partial("_LoginPartial")

是否有任何关于何时应该使用哪一个的文档?

Is there any documentation yet on when which one should be used?

推荐答案

这实际上是一个非常有趣的问题和场景.在某种程度上,异步是新的热点(虽然它真的不是那么新).Entity Framework 6 使用了异步方法,并且每一个......单个......片段......文档......突然开始对所有内容使用异步.我认为我们在这里看到了一些相同的情况.MVC 6 支持异步渲染部分,所以我的天啊,我们现在都必须使用异步.

This is actually a pretty interesting question and scenario. To a certain extent, async is the new hotness (though it's really not all that new). Entity Framework 6 hit with async methods and every... single... piece... of... documentation... suddenly starting using async for everything. I think we're seeing a little of the same here. MVC 6 supports async for things like rendering partials, so OMG we've all just have to use async now.

异步服务于一个非常具体的目的.它允许在当前任务处于等待状态时将活动线程返回到池中以执行其他任务.其中的关键部分是等待状态".某些任务完全与异步不兼容.像复杂的财务分析这样的 CPU 密集型工作永远不会允许线程进入等待状态,因此即使您将其设置为异步,所有内容也会有效地同步运行.另一方面,涉及网络延迟(从 Web API 请求资源、查询数据库等)或 I/O 绑定(读取/写入文件等)的事情有时可能会在线程处于在继续处理之前等待其他某个进程完成.

Async serves one very specific purpose. It allows the active thread to be returned to the pool to field other tasks while the current task is in a wait state. The key part of that is "wait state". Certain tasks are just flat out incompatible with async. CPU-bound work like complex financial analysis never allows the thread to enter a wait state so everything is effectively run as sync even if you set it up as async. On the other hand, things involving network latency (requesting a resource from a web API, querying a database, etc.) or that are I/O bound (reading/writing files, etc.) can at times have periods where the thread is waiting around for some other process to complete before it continues processing.

特别关注渲染部分,唯一不完全受 CPU 限制的部分是从文件系统读取视图文件本身.虽然这在技术上足以使其符合异步条件,但读取本质上可能小于 50KB 的文本文件真正需要多长时间.当线程被交还给池时,可能是时候请求它回来了,所以你实际上在那个时候使用资源的效率更低.

Looking specifically at rendering a partial, the only piece that's not entirely CPU-bound is reading the view file itself from the filesystem. While that's technically enough to make it eligible for async, how long is it really going to take to read what's essentially a text file that's probably less than 50KB max. By the time the thread is handed back to the pool, it's probably time to request it back, so you're actually using resources more inefficiently at that point.

总而言之,不要陷入可以异步完成,所以我必须异步完成"的陷阱.每次使用都应根据其是否具有实际价值进行评估.异步有很多开销,如果你只是在谈论几毫秒的等待时间,那么所有额外的开销可能不值得.

Long and short, don't fall into the trap of "it can be done async, so I must do it async". Each use should be evaluated in terms of whether there's actually value in it. Async has a lot of overhead, and if you're only talking about a few milliseconds of wait time, it's probably not worth all that extra overhead.

这篇关于何时在 MVC 6 的视图中使用 @await Html.PartialAsync的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆