I have the following code which checks an exception and if it is a 404 exception, it will check a list of urls to see if the page has been moved and if it matches, will issue a permanent redirect to the new page:
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);
}
}
}
Now I would expect that after the redirect has happened, non of the code after it would be executed ie the LogError
function wouldn't be called. But it seems to be as I am getting error messages for the pages not being found.
Is this standard behaviour for MVC Response.RedirectPermanent
?