简体   繁体   English

ASP.NET MVC应用程序不输出内容类型标头

[英]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.

相关问题 如何在 ASP.NET MVC 中使用 GET 请求发送 Content-Type 标头? - How can I send Content-Type header with a GET request in ASP.NET MVC? ASP.NET Core 中内容类型“application/csp-report”的“415 Unsupported Media Type” - "415 Unsupported Media Type" for Content-Type "application/csp-report" in ASP.NET Core 如何在 ASP.NET MVC 上将 Content-Type 设置为完全“文本/纯文本”? - How to set Content-Type to exactly “text/plain” on ASP.NET MVC? 如何在 ASP.NET Core MVC controller 中为重定向设置 Content-Type - How to set Content-Type for a Redirect in ASP.NET Core MVC controller ASP.NET 核心 2 - 缺少内容类型边界 - ASP.NET Core 2 - Missing content-type boundary InvalidOperationException:错误的内容类型:ASP.NET Core - InvalidOperationException: Incorrect Content-Type: ASP.NET Core 如果我的 Z099E7396E 中根本没有 Content-Type,ASP.NET 核心 Web API 管道的流程是什么? - What is the flow of ASP.NET Core Web API pipeline if I do not have Content-Type at all in my header? 删除 .NET Core 中的 Content-Type 标头 - Removing Content-Type header in .NET Core ASP.Net Core 为 JSON 内容设置 Content-Type 和 Location 标头 - ASP.Net Core set Content-Type and Location headers for JSON content 尝试使用Ajax将png上载到Asp.net Core服务器时出现“错误的Content-Type:image / png” - “Incorrect Content-Type: image/png” when trying to upload png with Ajax to Asp.net Core server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM