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");
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>
Related
I have my web.config setup as follows:
<customErrors mode="RemoteOnly" redirectMode="ResponseRedirect" defaultRedirect="~/SpecialPages/PageNotFound.aspx">
<error statusCode="404" redirect="~/SpecialPages/PageNotFound.aspx" responseMode="Redirect"/>
</customErrors>
But when a user goes to mysite.com/gibberish, my 404 page shows up, but the url stays as mysite.com/gibberish, but I want it to say mysite.com/SpecialPages/PageNotFound. Is there somethng else I am missing?
I'm using Kentico10 CMS if that makes any difference but have been following their instructions too. Seems like I'm missing something server side.
Have you specified Page not found in Kentico? If so, remove that configuration beacuse you don't want Kentico to handle error for you and then your custom errors in web.config should work as you set them up as in any other project.
In case anybody is reading I got an answer from kentico themselves:
This behavior is correct from SEO point of view. Previously we had the
behavior you wanted but it was really bad for SEO to do a redirection
so it was changed and just the 404 status code is returned and the URL
is the same. If you want to change this behavior, you can create
custom event handler and in the request end event check the status
code and if it is 404, do a redirection - but this can harm your SEO
rankings.
https://docs.kentico.com/k11/custom-development/handling-global-events/reference-global-system-events#Reference-Globalsystemevents-RequestEvents
I have Asp.net website (Webforms). I want to Display custom page for 404 errors. I have following entry in web.config file:
<customErrors mode="On" defaultRedirect="~/error.aspx">
<error statusCode="404" redirect="~/404error.aspx"/>
</customErrors>
But this only redirects all pages with extensions. But Extension-less URLs like [http://website/abc/123] are not considered as page request, therefore there's no mapping for 404 custom page display. Instead I get browser generated 404 error page.
I have also tried to use Global.asax file's Application_Error method, but this error doesn't fall in this method for this case.
Please suggest to capture such 404 exceptions.
Note: I have read the following Link that also discussed the simillar situation, but I couldn't understand the resolution for this. I want this to happen even with Visual Studio Debugging.
Thanks
Actually I had to put code for displaying 404 error in my custom URLRewrite module. Where I handled all extension less URLs & finally at the end where url has been checked for all cases, I rendered 404 Error page, as that url was not meant for my website.
Thanks
if the user type
http://myweb/mysite.aspx (file does not exist)
I want them to go to
http://myweb/site.aspx (file does exist)
My goal is to make a bilingual website (including url) but without having to make physical file
this would be one file
http://myweb/acceuil.aspx
http://myweb/home.aspx
Not sure what you are trying to do, but this is the best turtorial for the question you asked:
How to: Use Routing with Web Forms
That's not routing; that is a redirect.
_rick_shott seems to have the mojo on routing bad urls into a 301 redirect. I upvoted his answer. You should check out his HTTPModule solution.
In your web.config, add a customErrors and error node as follows:
<customErrors mode="On" defaultRedirect="ErrorDisplayPage.aspx">
<error statusCode="404" redirect="http://myweb/site.aspx"/>
</customErrors>
This will displayErrorDisplayPage.aspx for all unmanaged errors except for 404 errors (which are "page not found"). For 404 errors, the browser will redirect to the site.aspx page.
I've got this in my web.config and it's being hosted by the DiscountASP.net ISP
<customErrors mode="On" defaultRedirect="">
<error statusCode="404" redirect="404.aspx"/>
<error statusCode="500" redirect="404.aspx"/>
</customErrors>
I am hosting the site on DiscountASP.net and they also tell you to config it this way. I'm using Enterprise Library but I don't think that should make a difference. I don't believe I need to config anythign for a 404 in EL.
When my page loads with an error, my 404.aspx doesn't show and I get the default custom errors off message. I do not know why I don't get my 404.aspx page showing and get this instead:
Runtime Error
Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.
Details: To enable the details of this specific error message to be viewable on remote machines, please create a tag within a "web.config" configuration file located in the root directory of the current web application. This tag should then have its "mode" attribute set to "Off".
Notes: The current error page you are seeing can be replaced by a custom error page by modifying the "defaultRedirect" attribute of the application's configuration tag to point to a custom error page URL.
Have you tried using a relative path to see if that makes a difference?
<error statusCode="404" redirect="~/404.aspx"/>
Alternately, try an absolute path:
<error statusCode="404" redirect="http://www.domain.com/errors/404.aspx"/>
EDIT: As others have pointed out, and based on your comment to another answer, the 404 error page should be displayed when someone navigates to a page that doesn't exist, whereas general errors on a page could be captured by the defaultRedirect. So if you're testing the 404 then make sure you're testing with a nonexistent page. To test the defaultRedirect then have one of your pages throw an exception etc.
Is it possible that you are overriding this in another web.config, say in a sub folder? Or we can go the other way. Are you sure this is in the application root?
Is there any reason why you don't want to use the defaultRedirect for 500 errors?
You are FTP-ing the files correct? Try uploading the file as a binary file instead of text. You might be suffering from an encoding problem. I'm willing to bet right now that nothing you are doing in your web.config is working.
I found a solution here.
The real catch was using this:
Response.TrySkipIisCustomErrors = true;
I'm working on IIS6, ASP.NET, VS2008. The web site uses a custom error handler to catch 404 errors and serve an alternate page. When presented with an url of the form:
http://srv/crimson/articles/index
Everything works perfectly. But an url of the form:
http://srv/crimson/blog.aspx
Where blog.aspx does not exist, fails with the following message:
Server Error in '/Crimson' Application. Description: HTTP 404...
When I try and debug, none of the breakpoints in my 404 handler are hit. So it appears that something is catching the request earlier. Where? And how to I get it to pass the request on to my handler?
Edit
Thanks to those who answered, but none of those ideas worked. I've decided to attack the problem another way.
You might wanna try this:
In IIS6:
open "properties" for your website
go to "home directory" tab
click on "configuration"
look for the extension ".aspx"
click on "edit"
check the checkbox which says "verify that file exists"
edit
And what about this:
http://msdn.microsoft.com/en-us/library/h0hfz6fc(VS.80).aspx
<customErrors defaultRedirect="sorry.htm" mode="On">
<error statusCode="404" redirect="NotFound.aspx"/>
</customErrors>
Since 'RemoteOnly' specifies that custom errors are shown only to the remote clients, and that ASP.NET errors are shown to the local host.
hmm, Assaf is right, but to add to his answer I need to post some code.
Yes, Assaf does mean something else ASP.NET offers it's own error handling, configured through the web.config. You can either manage this through the IIS Admin snap in, or directly in the web.config file.
Within the <system.web> element you should have:
<customErrors defaultRedirect="sorry.htm" mode="RemoteOnly">
<error statusCode="404" redirect="NotFound.aspx"/>
</customErrors>
You can configure a different page for each HTTP error code, or let the default redirect handle them all.
You'll find that you do indeed need to set these error pages up in both the IIS custom errors and the ASP.NET configuration, as otherwise you'll end up in this situation - some pages go to your 404, and others use a default that you've not customised.
You should also make sure that your custom 404 page actually returns a 404 header to ensure that search engines etc treat it correctly.
Response.StatusCode = 404;
Response.StatusDescription = "Not found";
ASP.Net has a custom errors configuraiton of its own.
Go to the ASP.Net configuration of your virtual directory (web application) and right-click -> ASP.Net -> Edit Configuration -> Custom Errors.
Maybe you got a 500 so redirect for 404 is not triggered. You may focus on this 500 error at first.