Is there any function similar to die()
or exit()
from PHP in ASP.Net?

- 11,809
- 23
- 69
- 101
4 Answers
It depends on where you are in your code, but you want a way of getting the Response
object.
From here, you can call HttpResponse.End()
.
This method sends the buffer to the client, raises the EndRequest event and throws a ThreadAbortException
to stop the rest of the page executing.

- 13,925
- 11
- 59
- 92
You're looking for Response.End()
The End method causes the Web server to stop processing the script and return the current result. The remaining contents of the file are not processed.
The documentation for php's exit()
states:
Terminates execution of the script. Shutdown functions and object destructors will always be executed even if exit is called.
exit is a language construct and it can be called without parentheses if no status is passed.
They're not exactly equivalent, but that's mainly down to the differences between the way the two frameworks work.
You've got to take care when calling Response.End because it will throw an exception if there is any script left to process (Response.Redirect exhibits the same behaviour). This can cause you performance problems if this is done frequently.

- 766
- 10
- 12

- 21,196
- 13
- 77
- 99
-
1Although this might work, this is designed for use with classic ASP, not ASP.NET. You'd have to put this in your `.aspx` or `.cshtml` file, rather than in your C# code. – Connell Oct 03 '13 at 11:07
-
Response.End works in the code behind, for example you can do this after serving up a file to ensure that asp doesn't attempt to render the page. This would be admittedly dirty though. – Doctor Jones Oct 03 '13 at 11:10
You can use Response.Close(). This will stop execution of next statements.

- 1,460
- 3
- 18
- 34
-
Close will close the stream to the client, terminating the connection and often not rendering anything. – NiKiZe Oct 05 '22 at 11:27
You can use return;
within razor CSHTML. This will end execution for the rest of file

- 1,169
- 1
- 14
- 33