Asp.net mvc 3 在向MVC3发送HTTP标头后无法重定向
在基本控制器上使用RedirectPermanent(url)方法时,我在MVC3应用程序中遇到此错误。缓冲其他StackOverflow答案建议的输出没有任何效果Asp.net mvc 3 在向MVC3发送HTTP标头后无法重定向,asp.net-mvc-3,response.redirect,Asp.net Mvc 3,Response.redirect,在基本控制器上使用RedirectPermanent(url)方法时,我在MVC3应用程序中遇到此错误。缓冲其他StackOverflow答案建议的输出没有任何效果 protected void Application_BeginRequest(object sender, EventArgs e) { Context.Response.BufferOutput = true; } 重定向作为此网站上默认控制器的第一个操作结果被触发: 我没有任何直接添加头的代码。在这种情况下,有没有办
protected void Application_BeginRequest(object sender, EventArgs e)
{
Context.Response.BufferOutput = true;
}
重定向作为此网站上默认控制器的第一个操作结果被触发:
我没有任何直接添加头的代码。在这种情况下,有没有办法确定添加响应头的内容
这是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
看来我已经解决了这个问题。还有另一个重定向,它基于隐藏在NoCacheAttribute筛选器中的主机名。这是在OnResultExecuting方法中设置响应对象的属性 我将该方法重命名为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);