【问题标题】:Setting the Content Type of a ViewResult设置 ViewResult 的内容类型
【发布时间】:2010-12-27 08:15:27
【问题描述】:

我正在尝试创建一个 ActionResult,它的行为方式与 ViewResult 完全相同,但允许我指定 Content-Type 标头。这很困难,因为据我所知,ASP.Net MVC 交给 webforms 渲染引擎,然后使用页面上的标题设置内容类型,这种方法对我来说不可行,如果我会容易得多可以在一个中心位置改变这一点。

我尝试了以下实现,但它不起作用。 WebForms 引擎重置类型。如果之后设置也会发生同样的情况。或者,如果您尝试使用 Headers.Add 欺骗它

public class MyViewResult : ViewResult
{
    public override void ExecuteResult(ControllerContext context)
    {
        context.HttpContext.Response.ContentType = "myContent";
        base.ExecuteResult(context);
    }
}

【问题讨论】:

    标签: asp.net-mvc http-headers content-type


    【解决方案1】:

    尝试在 ExecuteResult 方法中交换两行的顺序。

    【讨论】:

      猜你喜欢
      • 2015-03-10
      • 2012-12-29
      • 2012-10-19
      • 2013-03-15
      • 2016-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多