I am trying to get WriteSubstition to work in MVC 5, and although it does the substitution, it does it at the beginning of the request, and not at the place where the substitution was called.
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 called in 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 this without the extension method by calling WriteSubstitution inline. When the page is displayed, the replaced 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 mistake in WriteSubstitution , or am I doing something wrong?
source
share