I am trying to resolve an issue that involves displaying helpful errors to users while my application is in startup. For example, when a database connection string is improper, the current behavior is that the database connection checking will throw an InvalidOperationException inside the Startup.cs file, which will prevent the host from being created. This causes a 500.30 ASP.NET Core App Failed To Start error page to be displayed in the browser. What I would like to do instead of this is to capture that exception, and open instead my own errorPage.html (or something similar).
Is it possible to open up a page inside of the Startup.cs file of an application? If so, how can I do that? And if not, what would be the best practice on displaying a custom error page so as to not be shown the default page?
Related
I am attempting to create a custom error page display to run in my app when there is an issue with the database connection string.
When I alter the string to something invalid, I receive the error mentioned in the title above.
Is there any way to override this page and show a more informative one that would tell me that my DB connection string is wrong?
There is a InvalidOperationException that is thrown in the Startup.cs file, but I'm unsure on how to extract this from the startup file and use it, when my app fails to start in the first place.
Is this possible to do?
You can disable the default error page by using the disableStartUpErrorPage="true" setting in web.config for the IIS hosting module. This will just fallback to another custom error page of your choosing served by IIS, rather than allowing you to show a dynamic custom page.
Documentation
I have configured the HttpErrors section in my web.config.
My custom error page is shown, but not when an exception occurs outside a web request (eg: Application startup, module initializing...), I get the following error:
The page cannot be displayed because an internal server error has occurred.
How can I make sure that my custom error page is ALWAYS shown?
You can't. The customer error pages are being configured at the start of the ASP.NET application, which fails in your case. There is no ASP.NET pipeline to handle the error pages.
Usually there is not much to do, besides making a proxy that filters out HTTP 500 error messages and replaces it with another message.
I'm facing a really weird scenario here with my local IIS. I have hosted multiple sites in the default website in my local IIS. One of them has the login page. From the login page, I'm redirecting the user to another page that is located in another site (which is also hosted in the same IIS inside default website virtual directory).
Now in the submit button click event of my login page, after authenticating the user, I have written a "Response.Redirect(redirect_url)". the redirect_url is being formed dynamically and given as a parameter to the Redirect method.
While debugging, the final redirect_url that is being sent as parameter to Redirect method is:
http://localhost/CP/web/console/console.aspx?sk=3e3cc1a8-73c4-4945-b3f8-08af22ea4324.50008
But after I try to go to the next step, I'm suddenly getting a HTTP 404 error saying that the resource doesn't exist and I have observed that Requested URL shown in the error page is different that what was dynamically sent to the Response.Redirect(...) method.
In the error page, the requested url shows the value as
http://localhost/CP/web/console/localhost/CPLogin?err=5
whereas my actual requested url formed in the code is:
http://localhost/CP/web/console/console.aspx?sk=3e3cc1a8-73c4-4945-b3f8-08af22ea4324.50008
I'm just unable to understand why the requested url is getting changed automatically.! Also, I observe that "localhost" is being appended to the requested URL again which is not what is supposed to happen.
Please visit THIS link[^] to understand this question more clearly. I have added screen shot of the error page.
http://amoghnatu.wordpress.com/2013/09/16/question-please-help-iis-throwing-http-404-not-found-but-requested-resource-actually-exists-requested-url-also-changing-automatically/[^]
Thanks a lot.!
Indeed, the problem was with the way I had hosted the sites in my application. I just removed all the sites related to my application from IIS and then hosted all of them again much more carefully. This resolved the "wrong redirect url" problem.
Also, I had some tables with missing required data because of which I was getting the error code.
So after I got all the tables filled with the required data and also after properly hosting the application in IIS, my problem got resolved.
In IIS, go to relevant folder, right click and "browse". Check out what is the URL. In most cases, this is due to the URL should have port number appended. For instance it will be something like :
http://localhost:<port number>/CP/web/console/console.aspx?sk=3e3cc1a8-73c4-4945-b3f8-08af22ea4324.50008
instead of
http://localhost/CP/web/console/console.aspx?sk=3e3cc1a8-73c4-4945-b3f8-08af22ea4324.50008
I have a .NET 4 application that uses Process.Start(URL); to open the user's default browser and take them to my update page if they accept an update request. This works fine for most people, but I'm getting crash logs from some users where this fails with:
System.ComponentModel.Win32Exception (0x80004005): The system cannot find the file specified
I have chastised myself for ignoring the possibility of failure here and using naive examples from the web and now I'm trying to work out what to do. My first instinct is to show a general "Couldn't open browser, here's the URL" message and maybe add a button to copy it to the clipboard, but can I do better?
A more robust way to open the URL? Although this seems to be the standard answer to questions about opening URLs, is it really the best way?
Something more informative to say to the user? Does failure mean a misconfiguration on the user's machine? Virus-scanner blocking access, maybe?
I have written a custom error page that displays an error message. Its an aspx page (and it needs to be).
The problem is that I tested the custom errors with a disabled database and the page just won't load. This is due (I think) to the fact that I have httphandlers and global.asax code trying to access the database so the app can never get to the error.aspx page if the db is down.
Anybody know how to disable all httphandlers and global.asax events from a page within an app?
Why don't you handle your error in Global_Asax and redirect the user to Error.aspx from Application_Error with a proper error message?