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

查看:966
本文介绍了何时在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")

vs.

@Html.Partial("_LoginPartial")

是否有关于何时使用哪个文档的文件?

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

推荐答案

这实际上是一个非常有趣的问题和场景.在某种程度上,异步是新的热点(尽管实际上并不是那么新).实体框架6受到异步方法的打击,文档的每一个...单篇...文档...突然开始对所有内容使用异步.我认为我们在这里看到了一些相同的地方. MVC 6支持异步处理,例如渲染局部图像,因此OMG我们都拥有现在就可以使用异步了.

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天全站免登陆