Reputation: 3748
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?
Upvotes: 7
Views: 8167
Reputation: 125488
I'd be more inclined to redirect them to a custom error page that clearly indicates that the file cannot be found, in the style of the rest of your web application.
You can specify how to handle certain errors in web.config
<customErrors mode="On">
<error statusCode="404" redirect="FileNotFound.aspx"/>
</customErrors>
Upvotes: 0
Reputation: 21654
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 :-)
Upvotes: 12