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?
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 :-)
throw new HttpException(404, "File not found");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With