TagHelper cached output by calling GetChildContentAsync() and Content.GetContent()

前提是你 提交于 2019-12-22 10:35:07

问题


According to this article if we use several tag helpers(targeted to the same tag) and in each of them we will use await output.GetChildContentAsync() to recieve html content we will come to the problem with cached output:

The problem is that the tag helper output is cached, and when the WWW tag helper is run, it overwrites the cached output from the HTTP tag helper.

The problem is fixed by using statement like:

   var childContent = output.Content.IsModified ? output.Content.GetContent() : 
                (await output.GetChildContentAsync()).GetContent();

Description of this behaviour:

The code above checks to see if the content has been modified, and if it has, it gets the content from the output buffer.

The questions are:
1) What is the difference beetween TagHelperOutput.GetChildContentAsync() and TagHelperOutput.Content.GetContent() under the hood?
2) Which method writes result to buffer?
3) What does it mean "cached output": ASP.NET MVC Core caches initial razor markup or html markup as result of TagHelper calling?

Thank in advance!


回答1:


The explanation of other answer was not clear for me, so i tested it and here is the summary:

  • await output.GetChildContentAsync(); ⇒ gets the original content inside the tag which is hard coded in the Razor file. note that it will be cached at first call and never changed at subsequent calls, So it does not reflect the changes done by other TagHelpers at runtime!

  • output.Content.GetContent(); ⇒ should be used only to get content modified by some TagHelper, otherwise it returns Empty!

Usage samples:

Getting the latest content (whether initial razor or content modified by other tag helpers):

var curContent = output.IsContentModified ? output.Content : await output.GetChildContentAsync();
string strContent = curContent.GetContent();


来源:https://stackoverflow.com/questions/38310227/taghelper-cached-output-by-calling-getchildcontentasync-and-content-getcontent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!