Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

respond with 404 error from asp.net page codebehind

Tags:

asp.net

I have a scenario in which I'm serving a file from codebehind.

which file, depends on request. in some cases there will be no file to serve and I want to return 404 to the browser.

how can I do that from codebehind? is this the correct course of action to show user there's no file available?

like image 521
Sumrak Avatar asked Jul 07 '09 21:07

Sumrak


2 Answers

you can use the Response.StatusCode property to return a 404:

Page.Response.StatusCode = 404

As for the question of whether it's the "correct thing to do" I'd say it depends how the Page is going to be accessed. If you're going to access it programmatically then yes I'd go with the 404. If however it is going to be a user facing system, then I'd go with a custom page of some sort. Programs like codes and humans like more understandable things :-)

like image 198
Doctor Jones Avatar answered Sep 22 '22 22:09

Doctor Jones


throw new HttpException(404, "File not found");
like image 39
SLaks Avatar answered Sep 23 '22 22:09

SLaks