[英]ASP.NET MVC Application not outputting content-type header
I currently have an issue with a legacy asp.net mvc 1 application whereby on a POST action on a controller, the content-type header in the response is missing 我目前有一个旧版asp.net mvc 1应用程序的问题,即在控制器上执行POST操作时,响应中的内容类型标头丢失了
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
This is normally not a problem, but we are now putting an Apache Reverse Proxy in front of the webserver - and mod_rewrite/mod_proxy is defaulting the content type to text/plain when there is no content-type defined from the downstream application. 通常这不是问题,但是我们现在将Apache反向代理放置在Web服务器的前面-当没有从下游应用程序定义的内容类型时,mod_rewrite / mod_proxy会将内容类型默认为文本/纯文本。
So while we are looking at options in terms of config on the Apache side (we have other application behind the reverse proxy, we we're mindful that we don't break something else in attempting to fix this), I would like to know what would the options be from an application / MVC point of view. 因此,虽然我们在Apache方面以配置的方式查看选项(我们在反向代理后面有其他应用程序,但我们谨记我们在尝试解决此问题时不会破坏其他东西),我想知道从应用程序/ MVC角度来看,这些选项是什么。 The code on my end has the following signature
我端的代码具有以下签名
[AcceptVerbs(HttpVerbs.Post)]
[ValidateAntiForgeryToken]
public ActionResult ProcessForm(FormCollection form, Model model)
{
// code here
return View("~/Views/viewpage.aspx");
}
What I have attempted to do so far is to add this bit of code in the header of viewpage.aspx 到目前为止,我一直试图将这部分代码添加到viewpage.aspx的标题中
<% HttpContext.Current.Response.AppendHeader("Content-Type", "text/html"); %>
But it had no effect. 但这没有效果。 Also updated the code from just returning a view to
RedirectToAction
, but it also didn't make any difference in terms of setting the content-type 还更新了代码,仅从将视图返回到
RedirectToAction
,但在设置内容类型方面也没有任何区别
Not sure if it is available/works with MVC1 不知道它是否可用/与MVC1一起使用
You may want to try return ContentResult instead of view. 您可能想尝试返回ContentResult而不是视图。 which you can specify content type to the ActionResult filter.
您可以为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();
}
}
then 然后
[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");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.