【发布时间】:2014-08-27 17:00:28
【问题描述】:
我有以下代码检查异常,如果是 404 异常,它将检查 url 列表以查看页面是否已移动,如果匹配,将发出永久重定向到新页面:
protected void Application_Error(object sender, EventArgs e)
{
var exception = Server.GetLastError();
if (exception != null)
{
if (exception is HttpException)
{
TryRedirect((HttpException)exception);
}
LogError(exception);
}
}
private void TryRedirect(HttpException httpException)
{
if (httpException.GetHttpCode() == 404)
{
WebsiteRedirect redirect = SiteCache.Redirects.FirstOrDefault(x => string.Compare(x.LegacyURL, HttpContext.Current.Request.RawUrl, true) == 0);
if (redirect != null)
{
// 301 it to the new url
Response.RedirectPermanent(redirect.NewURL);
}
}
}
现在我希望在重定向发生后,不会执行它之后的代码,即不会调用LogError 函数。但这似乎是因为我收到了找不到页面的错误消息。
这是 MVC Response.RedirectPermanent 的标准行为吗?
【问题讨论】:
标签: asp.net-mvc-4 redirect http-status-code-301