📜 ⬆️ ⬇️

C #: Etudes, part 3

In the previous part , three different solutions were proposed.

Today - a new mystery from the field of ASP.NET. Its main difference from the previous ones is that I ran into it in a real situation, and I had to spend time to figure out what was wrong.

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



protected void Page_Load( object sender, EventArgs e)<br>{<br> try <br> {<br> if ( object .ReferenceEquals(Request[ "ID" ], "aaa" ))<br> {<br> Response.Redirect( "~/PageA.aspx" );<br> }<br> else if (Request[ "ID" ] == "bbb" )<br> {<br> Response.Redirect( "~/PageB.aspx" );<br> }<br> else <br> {<br> int i = int .Parse(Request[ "ID" ]);<br> object boxedI = i;<br> if ((i > 5) && (( long )boxedI == 10))<br> {<br> Response.Redirect( "~/Page10.aspx" );<br> }<br> Response.Redirect( "~/PageDafault.aspx" );<br> }<br> } <br> catch (Exception)<br> {<br> Response.Redirect( "~/Error.aspx" );<br> }<br>}<br> <br> * 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 are:
Question on the "top three": where will each of them lead? >>> Any link will lead to Error.aspx <<<

A question for the Quartet: why so? >>> Response.Redirect interrupts the flow of the page code execution, throwing a ThreadAbortException for this. catch(Exception) catches it, throwing it again in Response.Redirect , but directing it to another page <<<
')
Question on the "five": how do you propose to correct the situation? >>> Most of all I liked the wdk method: just catch the necessary 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: interrupts the execution of the code pages. Since a ThreadAbortException exception is thrown at the end of the catch , this will not lead to the execution of unnecessary code outside the try block and in other page events. And the decision habrahabr.ru/blogs/net/77154/#comment_2245598 just leads to such an implementation, so I do not like <<<

It seems that the format of riddles chosen by me is not quite suitable for a .NET blog, so look for the following riddles on my blog :)

Thank you all!

Source: https://habr.com/ru/post/77154/


All Articles