Proper way to escape remaing code after Response.RedirectToRoute

dreadman

New Member
I'm writing an asp.net 4.5 application using the new routing features. I have a page that displays some information about an item. In the \[code\]Page_Load\[/code\] event I check the route data (item id) and user permissions, and if something isn't right (e.g. the id is for a deleted item) I use \[code\]Response.RedirectToRoute\[/code\] to send them packing, right back to the home page. Do not pass GO, do not collect $200.This made perfect sense until I tried to access a deleted item and instead of the home page I got an error page. I did some digging and discovered that even after I use \[code\]RedirectToRoute\[/code\] (unlike \[code\]Redirect\[/code\]) the rest of the page code continues to execute, which at the very least seems wasteful (since I'm just going to throw away the results) and throws errors when the necessary data doesn't exist.I did a little more SO mining and discovered the incredible evil that is \[code\]Response.End()\[/code\]. It does what I need, but even the MSDN page tells me that \[code\]Response.End\[/code\] is the bastard child of an ancient accursed language and isn't fit to see the light of day. The primary objection seems to be the fact that Response.End throws an exception, and that's bad for performance. I'm not the most experienced developer, so I don't understand the issue entirely, but I have trouble believing that throwing an exception is more expensive than loading the entire web page. The workarounds seem rather complex and excessive for a task so simple, especially since most pages require some kind of validity check.What am I supposed to do in this situation? Use \[code\]Response.End\[/code\] and beg forgiveness for my insolence? Cobble together some ugly workaround? Or is my perspective on the problem all wrong to begin with? I'd really like to know.
 
Back
Top