Kamran Ahmed
Kamran Ahmed

Reputation: 12440

die() or exit() functionality in ASP.NET

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

Upvotes: 11

Views: 22836

Answers (4)

Doctor Jones
Doctor Jones

Reputation: 21674

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.

Upvotes: 9

FindOutIslamNow
FindOutIslamNow

Reputation: 1236

You can use return; within razor CSHTML. This will end execution for the rest of file

Upvotes: 0

Parth Pandya
Parth Pandya

Reputation: 1490

You can use Response.Close(). This will stop execution of next statements.

Upvotes: 0

Connell
Connell

Reputation: 14419

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.

Upvotes: 11

Related Questions