I'm trying to get WriteSubstition to work in MVC 5, and while it performs the substitution, it is doing so at the beginning of the request, instead of at the location the substitution was invoked.
I used the following extension method:
public static class HtmlExtensions
{
public delegate string CacheCallback(HttpContextBase context);
public static object Substitution(this HtmlHelper myHtml, CacheCallback cacheCallback)
{
myHtml.ViewContext.HttpContext.Response.WriteSubstitution(
c => HttpUtility.HtmlEncode(
cacheCallback(new HttpContextWrapper(c))
));
return null;
}
}
And invoked it in the Index.cshtml as follows:
<div class="row">
<div class="col-md-4">
<h2Test</h2>
@DateTime.Now
@Html.Substitution(ctx => DateTime.Now.ToString())
@{Html.ViewContext.HttpContext.Response.WriteSubstitution(c => DateTime.Now.ToString());}
</div>
</div>
As you can see, I also tried it without the extension method, calling WriteSubstitution inline. When the page renders, the substituted blocks are at the beginning of the request:
10/10/2014 11:33:46 AM10/10/2014 11:33:46 AM<!DOCTYPE html>
<html>
Is this a bug in WriteSubstitution, or am I doing something wrong?