MVC3剃须刀 - Response.WriteSubstitution总是显示我的文字ontop页面 [英] MVC3 Razor - Response.WriteSubstitution always displays my text ontop of the page

查看:263
本文介绍了MVC3剃须刀 - Response.WriteSubstitution总是显示我的文字ontop页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


亲爱的开发家伙:)

我学习ASP.NET MVC3和我被困,当我使用Response.WriteSubsitution()方法。

每次我尝试在页面中使用它,则取代的文字总是出现在页面(截图顶部这里)。

考虑到我在我的控制下code:

 公共类HomeController的:控制器
{
        公众的ActionResult指数()
        {
            ViewBag.Message =欢迎使用ASP.NET MVC!;
            返回查看();
        }        [的OutputCache(持续时间= 20)]
        公众的ActionResult关于()
        {
            ViewBag.Date = DateTime.Now;
            返回查看();
        }
}

code在About.cshtml:

  @using MvcApplication1;
@ {
    ViewBag.Title =关于我们;
}< H2>关于< / H>
&所述p为H.;
    日期:@ ViewBag.Date< BR />
    随机数取代的:@ {Response.WriteSubstitution(MvcApplication1.Helpers.Test); }
&所述; / P>

我的Helper类:

 命名空间MvcApplication1
{
    公共静态类助手
    {
        公共静态字符串测试(HttpContext的背景下)
        {
            随机R =新的随机();
            返回r.Next(0,10)的ToString(CultureInfo.InvariantCulture);
        }
    }
}

我错过了什么?

谢谢!

修改与解决方案:

我解决与@Darin季米特洛夫的解决方案的问题。

有关的人在我不变的情况下,这是我的新code。

我的控制器:

  [DonutOutputCache(持续时间= 10)]
        公众的ActionResult关于()
        {
            ViewBag.Date = DateTime.Now;
            返回查看();
        }        公共字符串RandomNumber()
        {
            随机R =新的随机();
            返回r.Next(0,10)的ToString(CultureInfo.InvariantCulture);
        }

MvcDonutCaching实现DonutOutputCacheAttribute我们必须使用,而不是内建的OutputCacheOutput类。

我的观点:

  @using MvcApplication1;
@ {
    ViewBag.Title =关于我们;
    布局=〜/查看/共享/手机/ Layout.cshtml
}< H2>关于< / H>
&所述p为H.;
    日期:@ ViewBag.Date< BR />
    随机数取代的:@ Html.Action(RandomNumber,真)
    @侧
&所述; / P>

包重载Html.Action的方法来控制缓存:)

由于哺养这个线程所有的人。


解决方案

  

我学习ASP.NET MVC3和我被困,当我使用Response.WriteSubsitution()方法。


忘记MVC 3此方法作为ASP.NET的菲尔哈克解释。根本就不使用它。如果你想在ASP.NET MVC 3实现甜甜圈缓存,该框架拥有的没有向你提供。

第三方软件包,可以启用此功能,如果你不想卷你自己的。

. 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.

这篇关于MVC3剃须刀 - Response.WriteSubstitution总是显示我的文字ontop页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆