在使用Html.Action的异常,当控制器饰的OutputCache attribure [英] exception on use of Html.Action when controller is decorated with outputcache attribure

查看:456
本文介绍了在使用Html.Action的异常,当控制器饰的OutputCache attribure的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我打电话 Html.Action 从控制部装饰有观点则抛出异常的的OutputCache 属性。但是,当我从控制器一切删除属性按预期工作。

我不希望删除的OutputCache属性,我不明白如何属性负责抛出异常。我该如何解决这个问题?

控制器:

[的OutputCache(位置= OutputCacheLocation.None,NoStore = TRUE)]
公共类的TestController:控制器
{
    公共PartialViewResult测试()
    {
        的Debug.WriteLine(测试);
        返回PartialView();
    }
}

查看:

 < D​​IV>
    <! - 选项卡1 - >
    @ Html.Action(测试)
< / DIV>

例外:

  {错误执行的处理程序System.Web.Mvc.HttpHandlerUtil + ServerExecuteHttpHandlerAsyncWrapper孩子的请求。}

的InnerException

  {孩子的行为是不允许进行重定向操作。}

更新
当我尝试禁用的OutputCache我只得到了异常。通过添加上述属性或持续时间设置为0。


解决方案

的,OutputCache属性生成一个隐藏的异常,因为没有指定期限财产

。然而,持续时间不能为0,这样做出的,OutputCache属性不是非常有用的我。我决定创建自己的非缓存属性,把工作的关心。 (见code以下)

使用这个属性,而不是OutputCacheAttribute解决了我的问题。

使用系统;
使用的System.Web;
使用System.Web.Mvc;命名空间Cormel.QIC.WebClient.Infrastructure
{
    公共类NoCacheAttribute:ActionFilterAttribute
    {
        公共覆盖无效OnResultExecuting(ResultExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
            filterContext.HttpContext.Response.Cache.SetValidUntilExpires(假);
            filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            filterContext.HttpContext.Response.Cache.SetNoStore();            base.OnResultExecuting(filterContext);
        }
    }
}

An exception is thrown when I call Html.Action from a view when the controller is decorated with the OutputCache attribute. But when I remove the attribute from the controller everything works as expected.

I do not want to remove the OutputCache-attribute and I don't understand how the attribute is responsible for throwing the exception. How do I solve this problem?

Controller:

[OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
public class TestController : Controller
{
    public PartialViewResult Test()
    {
        Debug.WriteLine("test");
        return PartialView();
    }
}

View:

<div>
    <!-- Tab 1 -->
    @Html.Action("Test")
</div>

Exception:

{"Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'."}

InnerException

{"Child actions are not allowed to perform redirect actions."}

Update I only get the exception when I try to disable the outputcache. Either by adding the above attribute or setting the duration to 0.

解决方案

The outputcache attribute generated a hidden exception because the Duration property was not specified. However the duration cannot be 0 so that made the OutputCache attribute not very usefull for me. I decided to create my own NoCache attribute to take care of the work. (See code below)

Using this attribute instead of the OutputCacheAttribute solved my problem.

using System;
using System.Web;
using System.Web.Mvc;

namespace Cormel.QIC.WebClient.Infrastructure
{
    public class NoCacheAttribute : ActionFilterAttribute
    {
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
            filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
            filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            filterContext.HttpContext.Response.Cache.SetNoStore();

            base.OnResultExecuting(filterContext);
        }
    }
}

这篇关于在使用Html.Action的异常,当控制器饰的OutputCache attribure的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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