How can I disable the output in the using-block (IDisposable
HtmlHelper
) of a razor page?
The following is a MCVE and not my exact scenario.
I have a litte helper for IDisposable
:
public class DisposableHelper : IDisposable
{
private readonly Action _endAction;
public DisposableHelper(Action beginAction, Action endAction)
{
_endAction = endAction;
beginAction();
}
public void Dispose()
{
_endAction();
}
}
And this extension method:
public static IDisposable BeginSupress(this HtmlHelper htmlHelper)
{
var originalWriter = htmlHelper.ViewContext.Writer;
return new DisposableHelper(() => htmlHelper.ViewContext.Writer = TextWriter.Null, () => htmlHelper.ViewContext.Writer = originalWriter);
}
My Razor View Looks like this:
@using (Html.BeginSupress())
{
<h1>This actually should have not been printed!</h1>
}
I expected that nothing is printed, but this string is printed:
<h1>This actually should have not been printed!</h1>