【问题标题】:What is analog of Response.AddHeader("Refresh", "10") in ASP. NET MVC5什么是 ASP 中的 Response.AddHeader("Refresh", "10") 的模拟。网络 MVC5
【发布时间】:2016-12-28 04:10:07
【问题描述】:

有人能告诉我ASP 中是否有Response.AddHeader("Refresh", "10") 的类似物。 NET MVC5,好吗?

我尝试过[OutputCache(NoStore = true, Location = OutputCacheLocation.Client, Duration = 10)],但它不起作用。

【问题讨论】:

  • 你可以在MVC5中使用Response.AddHeader()。你想达到什么目的?什么“不起作用”?
  • @Jasen 我认为应该是一些方法属性标签来做到这一点,对吧?
  • 直接在控制器动作中添加。或者制作自定义动作过滤器来获取属性样式。

标签: asp.net-mvc-5 page-refresh


【解决方案1】:

[OutputCache] 用于缓存操作的输出。 Duration 参数只是告诉它缓存该输出多长时间。 任何都与设置 HTTP 标头无关,当然也不会自动刷新页面。

Reponse.AddHeader 在 MVC5 中仍然有效;你只需要确保你还没有开始响应。除非你正在做一些不合时宜的事情,否则这并不难。例如,如果您要返回 ViewResult,只需先调用它:

Response.AddHeader("Refresh", "10");
return View();

如果您直接写入响应,则只需确保在开始之前添加标题即可。

【讨论】:

    【解决方案2】:

    您可以直接在控制器中使用它

    public ActionResult MyAction()
    {
        Response.AddHeader("Refresh", "10");
        return View();
    }
    

    或者您可以制作自定义操作过滤器

    public class RefreshAttribute : ActionFilterAttribute, IActionFilter
    {
        public string Duration { get; set; }
    
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            var duration = 10;
    
            Int32.TryParse(this.Duration, out duration);            
    
            filterContext.HttpContext.Response.AddHeader("Refresh", duration.ToString());
        }
    }
    

    用法

    [Refresh(Duration = "10")]
    public ActionResult MyAction()
    {
        return View();
    }
    

    【讨论】:

      猜你喜欢
      • 2011-09-18
      • 2017-03-27
      • 2017-10-15
      • 1970-01-01
      • 1970-01-01
      • 2011-06-28
      • 1970-01-01
      • 2012-01-25
      相关资源
      最近更新 更多