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!
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