MVC3 Razor - Response.WriteSubstitution always displays my text at the top of the page

. Dear girls :)

I am learning ASP.NET MVC3 and I get stuck when using the Response.WriteSubshib () method.

Every time I try to use it on a page, replaced text always appears at the top of the page ( screenshot here ).

Given that I have the following code in the 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 am solving a problem with @Darin Dimitrov's solution.

For people in the same case, 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 DonutOutputCacheAttribute class, we should use the built-in OutputCacheOutput instead.

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 the Html.Action method to control the cache :)

Thanks to all the people who feed this topic.

+6
source share
1 answer

I am learning ASP.NET MVC3 and I get stuck when using the Response.WriteSubshib () method.

Forget about this method in ASP.NET MVC 3 as Phil Haack explains . Just do not use it. If you want to achieve donut caching in ASP.NET MVC 3, the framework has nothing to offer you.

There are third-party packages that can enable this feature if you do not want to roll back your own.

+4
source

Source: https://habr.com/ru/post/919266/


All Articles