C# – PHP die(“reason”) equivalent in ASP.NET
by admin on Sep.11, 2013, under Programming
I was looking for an equivalent asp.net function to the php die(“reason”) function which is quite useful whet it comes to stopping the execution of a script and returning the reason. Unfortunately I couldn’t find a real alternative, but luckily this is not a difficult thing to do, since you can modify the Response as much as you want.
this.Response.Clear(); this.Response.ContentType = "text/html"; this.Response.StatusCode = 200; // 200 = OK this.Response.Write("Script execution halted."); this.Response.End();
Or if you’d like to build a real replacement for the die(reason) expression:
void die(string reason) { this.Response.Clear(); this.Response.ContentType = "text/html"; this.Response.StatusCode = 500; // 500 = Error this.Response.Write(reason); this.Response.End(); }
Now that’s easy, isn’t it?