简体   繁体   中英

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

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.

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. 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

<% 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

Not sure if it is available/works with MVC1

You may want to try return ContentResult instead of view. which you can specify content type to the ActionResult filter.

        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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM