在基本控制器上使用RedirectPermanent(url)方法时,我在MVC3应用程序中收到此错误。其他StackOverflow答案建议的缓冲输出没有任何效果。
protected void Application_BeginRequest(object sender, EventArgs e)
{
Context.Response.BufferOutput = true;
}
重定向作为此网站上默认控制器的第一个操作结果触发: http://www.autoquoter.com
我没有任何直接添加标题的代码。既然如此,有没有办法确定添加响应头的内容?
以下是来自webkit网站第一页的调试器的网络日志。
Request URL:http://www.autoquoter.com/
Request Method:GET
Status Code:301 Moved Permanently
Request Headers
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2
Response Headers
Cache-Control:no-cache, no-store, must-revalidate
Content-Length:128
Content-Type:text/html; charset=utf-8
Date:Thu, 21 Mar 2013 17:10:38 GMT
Expires:-1
Location:/aq/en/Home
Pragma:no-cache
Server:Microsoft-IIS/6.0
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:3.0
X-Powered-By:ASP.NET
答案 0 :(得分:2)
看起来我已经解决了这个问题。基于主机名的另一个重定向隐藏在NoCacheAttribute过滤器内。这是在OnResultExecuting方法中设置Response对象的属性。
我将方法重命名为OnActionExecuting,以便更快地触发,并用RedirectResult替换手动重定向。如果我已经重定向,我现在也避免更新缓存设置。
在:
if (currentHost != prefHost && filterContext.HttpContext.Response.StatusCode != 301)
{
var Url = filterContext.HttpContext.Request.Url.Scheme + "://" + prefHost + filterContext.HttpContext.Request.Url.PathAndQuery;
filterContext.HttpContext.Response.StatusCode = 301;
filterContext.HttpContext.Response.RedirectLocation = Url;
filterContext.HttpContext.Response.End();
return;
}
filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
filterContext.HttpContext.Response.Cache.SetNoStore();
后:
if (currentHost != prefHost && filterContext.HttpContext.Response.StatusCode != 301)
{
var Url = filterContext.HttpContext.Request.Url.Scheme + "://" + prefHost + filterContext.HttpContext.Request.Url.PathAndQuery;
filterContext.Result = new RedirectResult(Url, true);
disableCache = false;
}
if (disableCache)
{
filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
filterContext.HttpContext.Response.Cache.SetNoStore();
}
base.OnActionExecuting(filterContext);