3

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?

Pete
  • 57,112
  • 28
  • 117
  • 166

3 Answers3

4

Ok so as it turns out this is the standard behaviour as RedirectPermanent has 2 overloads (I never saw the second overload as my VS was playing up):

Response.RedirectPermanent(string url);
Response.RedirectPermanent(string url, bool endResponse);

The option of endResponse says that the permanent redirect will continue to finish the response or end the process immediately after the redirect.

The default is set to false meaning that the first overload (which is what I have used) will finish the response which is why it is calling the LogError function

Pete
  • 57,112
  • 28
  • 117
  • 166
-1

When you are using response.RedirectPermanent() it will completely delegates the request.It's not going to execute or process any statement after Response.RedirectPermanent(redirect.NewURL) statement. If you use Response.RedirectPermanent(string,boolean) method then give boolean value to true then it will execute your logerror(exception)

I request you to go through this link http://www.stepforth.com/blog/2008/redirects-permanent-301-vs-temporary-302/#.U7pxUfmSx-M

Nadendla
  • 712
  • 2
  • 7
  • 17
  • So why is the LogError function being called after the RedirectPermanent? – Pete Jul 07 '14 at 09:45
  • It permanently redirects the request i.e. you are not going to get the control back from where you called – Nadendla Jul 07 '14 at 10:02
  • 1
    This is completely wrong having read further into `RedirectPermanent`, it will continue to process the response unless you tell it not to - it takes an additional boolean into method which if set to true will terminate the current response. If set to false it will finish the current response. The default is set to false – Pete Jul 07 '14 at 10:09
  • Response.RedirectPermanent(url) will call Response.RedirectPermanent(url, false) meaning it will process everything after – Pete Jul 07 '14 at 10:17
  • yes i agree.because the default value is false. @Pete – Nadendla Jul 07 '14 at 10:18
-1
Response.RedirectPermanent(string url, bool endResponse);
return null;
boateng
  • 910
  • 11
  • 21