Reputation: 1825
Where are the default Descriptions stored for the various ASP.NET HTTP errors hosted on IIS? Can they be accessed via code?
EG. The default error page for a 404 will include the following text:
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Can we read this string via code?
I can catch an HttpException (I know a HttpException != HttpError) and do things like .GetHttpCode(), but I haven't seen anywhere detailing where the IIS error messages are stored and whether I can access via code.
It would be nice if I was able to reuse the default text in a custom error page when I don't want to come up with it myself.
UPDATE 1
Following one of the comments, I investigated IIS to see where it's Error Pages are configured. Sure enough, there is an Error Pages directory, defaulting to, for example "%SystemDrive%\inetpub\custerr\\404.htm", however these error messages are different from the ones that are shown by the web application (which I assume are generated by .NET).
EG
The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.
These error pages are physical pages hosted on the web server, so accessing them via code is not the option I am seeking.
Upvotes: 0
Views: 319
Reputation: 239764
As suggested in the comments, these strings are resources within the System.Web assembly, and there's no systematic means of retrieving just the ones related to HTTP error codes1 (versus all of the other string resources within System.Web)
For example, this Console application, with a reference to System.Web
:
using System;
using System.Resources;
namespace PlayAreaCSCon
{
class Program
{
static void Main(string[] args)
{
var rm = new ResourceManager("System.Web",
typeof(System.Web.HttpApplication).Assembly); //Arbitrary type
Console.WriteLine(rm.GetString("NotFound_Http_404"));
Console.ReadKey();
}
}
}
Generates this output2:
HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Note also that the fact that these strings are stored as resources is an implementation detail and not documented anywhere, so you'd have to accept that any code based on accessing these resources could be broken by any future updates to the .NET Framework (although it's unlikely to change in practice).
1By which I mean I had to use a decompiler to find the magic string "NotFound_Http_404" used in this sample. There's no logical way to go from that to the needed string for any other status code.
2On a machine where no effort has been made to select anything other than the en-US locale. YMMV.
Upvotes: 1