简体   繁体   English

在渲染之前/之后检查条件

[英]Checking condition before/after RenderPartial performance

Let's say there's a RenderPartial that checks if a certain condition is true, then renders the page. 假设有一个RenderPartial ,它检查某个条件是否为真,然后渲染页面。

Is there any performance benefit to check for the condition first, then call RenderPartial ? 首先检查条件,然后调用RenderPartial有什么性能上的好处? I'm sure that this technique is a performance benefit for RenderAction 's, since we can essentially short circuit going through the MVC lifecycle step. 我确信这项技术对于RenderAction来说是性能上的优势,因为我们可以在MVC生命周期步骤中实现本质上的短路。 But I'm thinking since RenderPartial "goes" straight to the view, it might not have the same benefit. 但是我在考虑,因为RenderPartial可以直接进入视图,所以它可能没有相同的好处。


Example (Checking the Condition in RenderPartial): 示例(检查RenderPartial中的条件):


myPage.cshtml myPage.cshtml

@model Namespace.AdObject

@{ Html.RenderPartial("~/Views/Ad.cshtml", Model); }

~/Views/Ad.cshtml 〜/ Views / Ad.cshtml

@model Namespace.AdObject

@if (Model != null && Model.AdScript != null)
{
    // render the ad
}

Example (Checking the Condition Before RenderPartial): 示例(在RenderPartial之前检查条件):


myPage.cshtml myPage.cshtml

@model Namespace.AdObject

@if (Model != null && Model.AdScript != null)
{
    Html.RenderPartial("~/Views/Ad.cshtml", Model);
}

~/Views/Ad.cshtml 〜/ Views / Ad.cshtml

@model Namespace.AdObject

// render the ad

RenderPartial() is a void method that writes to the response output stream. RenderPartial()是写入响应输出流的void方法。 The HTML output generated by executing the partial view is rendered into the calling (or parent) view. 通过执行部分视图生成的HTML输出将呈现到调用(或父)视图中。 Even If model is null , write will be time consuming and it reduces efficiency. 即使model为null ,写操作也会很耗时,并且会降低效率。 So checking the condition before RenderPartial is better in terms of performance. 因此,就性能而言,在RenderPartial之前检查条件会更好。

I would say checking for before the RenderPartial call is better. 我会说最好在RenderPartial调用之前进行检查。 Even if your Partial view is small it still comes down to an extra file system IO call to the .cshtml file from the Web Server which is a bottleneck to performance. 即使您的“部分视图”很小,它仍然可以归因于Web服务器对.cshtml文件的额外文件系统IO调用,这是性能的瓶颈。 When you check first you can avoid the IO call all together. 第一次检查时,可以避免一起进行IO调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM