29

I was wondering if it's possible to throw a 404 error from within a page(code behind)? Or possibly even throw any other type of error page such as 408(timeout) or 401(authentication required)?

Note: I don't just want to have the page return a status code of 404, I want it to use the ASP.Net(or my CustomErrors) 404 error page.

Something like this in the code behind:

if(id>10){ //if id is greater than 10, then it doesn't exist here
  throw 404Error();
}
Earlz
  • 62,085
  • 98
  • 303
  • 499

4 Answers4

44

You could throw an HttpException and set the corresponding status code:

throw new HttpException(404, "Not found");

It will also work with other status codes. Just a remark about the 401: as you probably know when ASP.NET MVC detects this code it automatically redirects you to the login page and getting a custom error page for the 401 status code could be a real PITA to implement.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Hmm. If this exception was thrown when CustomErrors is off, will it show the standard ASP.Net exception page with a stacktrace? – Earlz Feb 11 '11 at 22:08
  • 3
    @Earlz, if `customErrors="Off"` you will probably get an YSOD. – Darin Dimitrov Feb 11 '11 at 22:09
  • I was just curious for when in a development environment. That seems to work though! – Earlz Feb 11 '11 at 22:11
  • The exception thrown in this way is not transferred to the client. Is a server exception like any other exceptions – bubi Aug 02 '18 at 11:17
25

A much better way is:

// Throws a 404 Not found
Response.Clear();
Response.StatusCode = 404;
Response.End();

There is no need to throw an exception and the above works much better when using custom error pages in the Web.Config

phwt
  • 1,356
  • 1
  • 22
  • 42
George Filippakos
  • 16,359
  • 15
  • 81
  • 92
  • 4
    You can also use the enums as well in System.Net.HttpStatusCode. – Martin Dawson Apr 30 '16 at 20:53
  • 4
    `Response.End()` should be avoided as it's a carry-over from Classic ASP. It raises a `ThreadAbortException` which does not play nice in heavy-load environments. Instead call `HttpContext.Current.ApplicationInstance.CompleteRequest` and if necessary, set `Response.SuppressContent = true` after writing an error message. – Dai May 26 '17 at 08:58
  • This answer is especially helpful because is puts the code fragment into context by including the Clear() and End() calls. – Be Kind To New Users Sep 11 '17 at 22:12
  • 2
    If you're in `Page_Load` (or any pre-rendering code) You don't have to `Clear()` and `End()` - it will render the page and just set the 404 status on the response properly (if that's what you want). `Response.End()` is actually kind of bad. There's an excellent explanation here https://stackoverflow.com/a/3917180/396005 – Bron Davies Oct 31 '17 at 03:28
3

In regards of Page Load in ASP.NET WebForms needs some small workaround.

protected void Page_Load(object sender, EventArgs e)
{
    HttpNotFound();
}

private void HttpNotFound()
{
    Response.Clear();
    Response.StatusCode = 404;
    Response.End();
    HttpContext.Current.ApplicationInstance.CompleteRequest();
}
phwt
  • 1,356
  • 1
  • 22
  • 42
Pit J
  • 169
  • 8
0

If your client is capable of hitting the PHP page so code is executed, it means that you didn't get a 404 (client side error, "path/file not found"), nor did you get any other sort of 400-class error, BUT you are instead actually getting what should be classified as a 500 error of some sort.

The HTTP spec uses file/path as synonyms. This comes from the old days of the internet when it was super common for webservers to expose directory browsing lists.

djangofan
  • 28,471
  • 61
  • 196
  • 289