C #: Etudes, part 3

    In the previous part , three different inherent solutions were proposed.

    Today is a new mystery from the ASP.NET domain. Its main difference from the previous ones is that I came across it in a real situation, and I had to spend time to figure out what was the matter.

    So, on the page Test.aspx there is a code:



    protected void Page_Load(object sender, EventArgs e)
    {
      try
      {
        if (object.ReferenceEquals(Request["ID"], "aaa"))
        {
          Response.Redirect("~/PageA.aspx");
        }
        else if (Request["ID"] == "bbb")
        {
          Response.Redirect("~/PageB.aspx");
        }
        else
        {
          int i = int.Parse(Request["ID"]);
          object boxedI = i;
          if ((i > 5) && ((long)boxedI == 10))
          {
            Response.Redirect("~/Page10.aspx");
          }
          Response.Redirect("~/PageDafault.aspx");
        }
      }      
      catch (Exception)
      {
        Response.Redirect("~/Error.aspx");
      }
    }

    * This source code was highlighted with Source Code Highlighter.

    There are also four links:
    1. Test.aspx? ID = aaa
    2. Test.aspx? ID = bbb
    3. Test.aspx? ID = 4
    4. Test.aspx? ID = 10


    UPD So, the correct answers:
    Question to the "troika": where will each of them lead? >>> Any link will lead to Error.aspx <<<

    Question to the Quartet: why exactly? >>> Response.Redirectinterrupts the execution flow of the page code, throwing for this ThreadAbortException. catch(Exception)catches him, throwing him back in Response.Redirect, but directing to another page <<<

    Question to the "five": how do you propose to rectify the situation? >>> Most of all I liked the wdk method : just catch the right type of exception before catch(Exception): habrahabr.ru/blogs/net/77154/#comment_2246114 Despite the simplicity of this code, it does exactly what it needs: it interrupts the execution of the page code. Since the exception is thrown ThreadAbortException at the end of the blockcatch, this will not lead to the execution of unnecessary code outside the block try and in other page events. And the solution habrahabr.ru/blogs/net/77154/#comment_2245598 just leads to such an implementation, that's why I don’t like it <<<

    It seems that the format of the riddles I have chosen is not quite suitable for the .NET blog, so look for the following riddles in my blog :)

    Thank you all!

    Also popular now: