. Dear Dev Guys :)
I'm learning ASP.NET MVC3 and I'm stuck when I use Response.WriteSubsitution() method.
Every time I try to use it in the page, the substitued text always appears on top of the page (screenshot here).
Considering I have the following code in my controller:
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Message = "Welcome to ASP.NET MVC!";
return View();
}
[OutputCache(Duration=20)]
public ActionResult About()
{
ViewBag.Date = DateTime.Now;
return View();
}
}
Code in About.cshtml :
@using MvcApplication1;
@{
ViewBag.Title = "About Us";
}
<h2>About</h2>
<p>
Date : @ViewBag.Date<br />
Random Substitued number : @{ Response.WriteSubstitution(MvcApplication1.Helpers.Test); }
</p>
My Helper class:
namespace MvcApplication1
{
public static class Helpers
{
public static string Test(HttpContext context)
{
Random r = new Random();
return r.Next(0, 10).ToString(CultureInfo.InvariantCulture);
}
}
}
Did I miss something ?
Thanks !
EDIT with solution:
I solve the issue with the @Darin Dimitrov's solution.
For people in the same case of me, this is my new code.
My controller:
[DonutOutputCache(Duration = 10)]
public ActionResult About()
{
ViewBag.Date = DateTime.Now;
return View();
}
public string RandomNumber()
{
Random r = new Random();
return r.Next(0, 10).ToString(CultureInfo.InvariantCulture);
}
MvcDonutCaching implements the class DonutOutputCacheAttribute we have to use instead the build-in OutputCacheOutput.
My View :
@using MvcApplication1;
@{
ViewBag.Title = "About Us";
Layout = "~/Views/Shared/Mobile/Layout.cshtml";
}
<h2>About</h2>
<p>
Date : @ViewBag.Date<br />
Random Substitued number : @Html.Action("RandomNumber", true)
@Side
</p>
The package overloads Html.Action method to control the cache :)
Thanks all people that feed this thread.
I'm learning ASP.NET MVC3 and I'm stuck when I use Response.WriteSubsitution() method.
Forget about this method in ASP.NET MVC 3 as Phil Haack explains. Simply don't use it. If you wanna achieve donut caching in ASP.NET MVC 3, the framework has nothing to offer you.
There are third party packages that could enable this functionality if you don't want to roll your own.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With