2014-01-16 25 views
0

我现在有一个问题与由此在一个控制器上的POST操作的传统asp.net MVC 1应用,在响应中的Content-Type头缺少ASP.NET MVC应用不输出内容类型报头

Cache-Control:private 
Date:Thu, 16 Jan 2014 10:43:52 GMT 
Server:Microsoft-IIS/7.5 
Set-Cookie:cartguid=; expires=Mon, 16-Dec-2013 10:43:53 GMT; path=/; HttpOnly 
Transfer-Encoding:chunked 

这通常不是问题,但我们现在在Web服务器前放置一个Apache反向代理服务器 - 并且mod_rewrite/mod_proxy在没有从下游定义的内容类型时将内容类型默认为text/plain应用。

因此,当我们在Apache方面的配置方面寻找选项时(我们有其他应用程序在逆向代理背后,我们注意到我们不试图解决这个问题时会破坏其他的东西),我想知道从应用程序/ MVC的角度来看选项是什么。在我结束的代码具有以下签名

[AcceptVerbs(HttpVerbs.Post)] 
[ValidateAntiForgeryToken] 
public ActionResult ProcessForm(FormCollection form, Model model) 
    { 
     // code here 
     return View("~/Views/viewpage.aspx"); 
    } 

我试图到目前为止做的是添加的这段代码中的viewpage.aspx

<% HttpContext.Current.Response.AppendHeader("Content-Type", "text/html"); %> 

的头,但它没有任何效果。还更新了从刚刚返回一个以RedirectToAction的代码,但它也没有让在设置内容类型

+1

我不认为MVC1到MVC2是一个大的跳跃(它是MVC3带来了很大的变化)。也许你可以尝试升级,如果有MVC2修复?我认为MVC1在很多领域都存在问题。 –

回答

0

不知道它是可用的条件有什么区别与MVC1 /工作

您可能希望尝试返回ContentResult而不是视图。您可以将内容类型指定给ActionResult过滤器。

 public static string RenderViewAsString(string viewName, object model, 
     ControllerContext controllerContext, ViewDataDictionary viewData, TempDataDictionary tempData) 
    { 
     viewData.Model = model; 
     using (var stringWriter = new StringWriter()) 
     { 
      var result = ViewEngines.Engines.FindPartialView(controllerContext, viewName); 
      result.View.Render(
       new ViewContext(controllerContext, result.View, viewData, tempData, stringWriter), 
       stringWriter); 
      return stringWriter.GetStringBuilder().ToString(); 
     } 
    } 

然后

[AcceptVerbs(HttpVerbs.Post)] 
[ValidateAntiForgeryToken] 
public ActionResult ProcessForm(FormCollection form, Model model) 
{ 
    // code here 
    var resultAsString = RenderViewAsString("~/Views/viewpage.aspx", null, ControllerContext, ViewData, TempData); 
    return Content(resultAsString, "text/html"); 
}