IIS 7 does not fire Application Error in Global.asax while migrating a HTML application to ASP.NET 4.0 - iis-7

I am migrating a HTML site to ASP.Net 4.0 web application. If someone types an existing HTML page URL, I want to redirect them to a corresponding ASP page.
I have already tried below suggestions, nothing worked out.
included Custom Errors tag which redirects to some asp page in web.config - not worked
enter code here
<customErrors mode="On">
<error statusCode="404" redirect="~/Error.aspx" />
</customErrors>
enter code here
included below code under Application_Error method in the Global.asax page. - It does not fire
enter code here
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
string fullOrginalpath = Request.Url.ToString();
string strPathExtn =
Request.CurrentExecutionFilePathExtension.ToString();
if (fullOrginalpath.Contains(".html"))
{
Server.ClearError();
Response.Clear();
fullOrginalpath = fullOrginalpath.Replace(".html", ".aspx");
Response.Redirect(fullOrginalpath);
}
}
enter code here
Tried having httpErrors tag in web.cofig - it throws 500 internal error.

I think you may want to look at the IIS URL Rewrite module:
https://www.iis.net/downloads/microsoft/url-rewrite

Related

Custom error not redirecting in web.config

In my web.config file, I have this:
<system.web>
<customErrors mode="On" defaultRedirect="~/SomePage.aspx" />
</system.web>
The problem is that it's not working, I'm still seeing the error pages instead of the custom error page.
What do I need to change?
After dealing with this issue on and off for a few days, I've figured out why things aren't working as expected. Check and see what's in your global.asax file; mine had the following:
void Application_Error(object sender, EventArgs e)
{
}
If the error is handled in the global.asax file at the application level, even if there's no actual code in the handler, then the settings in the web.config file are overridden. After I removed this empty block of code, everything worked as expected.

ASP.NET rewritten custom errors do not send content-type header

I have the following configuration in my web.config:
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Error/Error.html">
<error statusCode="404" redirect="~/Error/Error.html" />
<error statusCode="400" redirect="~/Error/Error.html" />
</customErrors>
FWIW, this is an ASP.NET MVC 3 application.
When I generate an error. For example by visiting..
http://testserver/this&is&an&illegal&request
.. which is blocked by ASP.NET request validation, the error page is returned, but there is no content-type header. IE infers the content and renders the HTML, however Firefix (correctly IMO) treats the content as text and displays the HTML code.
Are there additional steps that I need to take to persuade ASP.NET to send a content type header? I assume this is related to the fact that it's picking the files up from the file system, but the MIME types appear to be configured correctly on the server.
My ASP.NET MVC 2 application was sending the content-type header - Content-Type: text/html - correctly. Then it started giving this weird problem after upgrading the application pool from .Net Framework v2 to .Net Framework v4. I'm using the following configuration
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="/500.html">
<error statusCode="404" redirect="/404.html" />
</customErrors>
and I wanted to stick to static pages for my custom error pages.
The only solution I could think of was setting the header explicitly in the Application_Error method of the MvcApplication class in the Global.asax.cs file.
public class MvcApplication : System.Web.HttpApplication
{
// .
// .
// .
void Application_Error(object sender, EventArgs e)
{
// .
// Remember to set Response.StatusCode and
// Response.TrySkipIisCustomErrors
// .
Response.ContentType = "text/html";
// .
// .
// .
}
// .
// .
// .
}
A bit annoying but the simplest solution that came to my mind.
With a static file error page, I have not found any solution.
Using a dynamic error page, headers are correctly set. But you may lose the error status code in the process and should then set it yourself in the error page code.
(That is somewhat the answer of rangitatanz, but maybe written more explicitly.)
So here is an example solution I use (webform way):
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Error/unknown.aspx">
<error statusCode="404" redirect="~/Error/404.aspx" />
<error statusCode="400" redirect="~/Error/400.aspx" />
</customErrors>
And in page load (here for the 404 one):
protected void Page_Load(object sender, EventArgs e)
{
Response.StatusCode = 404;
Response.StatusDescription = "Not found";
Response.TrySkipIisCustomErrors = true;
}
This could be used in a MVC project. If you do not want to mix webforms and MVC, you could write an error controller for that with its associated views. (That is indeed the way I go in my MVC project, but currently I am back on a webform one and so I have re-solved that using webform.)
ASP.Net wont' be sending this IIS will. If you want ASP.Net to send it then try add an Error.aspx and see what that looks like?
What I mean here is that you will be sent to an html page - which unless you have wired up IIS to run these through aspnet then it will just be served up normally.
If you add a new page error.aspx you can a) check if ASP.Net has fixed your issue or b) manually add your headers in there.

ASP.Net 4: Custom error pages with correct HTTP status code, how?

I'm running a ASP.Net 4 site on Windows 2008 R2 with IIS 7.5.
When developing I use windows 7 and VS 2010 with IIS 7.5 as well.
I've setup my web.config as such:
<customErrors mode="On" defaultRedirect="~/Error500.aspx" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="~/Error404.aspx"/>
</customErrors>
And my Error404.aspx page has this in its code-behind:
protected void Page_Load(object sender, EventArgs e)
{
Response.StatusCode = 404;
}
On my developer machine this works fine, I see my pretty errorpage in the browser, and using Fiddler I can confirm I only receive the 404 HTTP status.
Now when I publish it to my server, it's a different story. Here I'll see the IIS' own 404 page ("C:\inetpub\custerr\en-US\404.htm")
If I remove the Response.StatusCode in my code-behind, I get the errorpage fine on the server, albeit with a 200 HTTP status.
What am I overlooking since this doesn't work ?
Does this article by Rick Strahl help? It covers 500 errors and the TrySkipIisCustomErrors property to override the default IIS behaviour.

asp.net not displaying a custom 404 page as configured

in my web config I have:
<customErrors mode="On">
<error statusCode="404" redirect="~/error/404.aspx" />
</customErrors>
http://localhost/meh <-- standard 404 is shown
http://localhost/meh.aspx <-- custom 404 is shown
http://localhost/error/404.aspx <-- the custom error page I want shown for all 404 errors
How do I have to setup my web.config to send all 404 to my custom error?
Thanks
You have to configure this in IIS. By default, only specific files will get routed through the ASP.NET framework... otherwise IIS will handle it.
Use the Application_Error event handler in the global.asax file to redirect the user to ~/error/meh.aspx
in global.asax
protected void Application_Error(object sender, EventArgs e)
{
Response.Redirect("~/error/404.aspx");
}
In your web.config, also add
<customErrors mode="On" defaultRedirect="/error/404.aspx" />

Accessing original URL in IIS7 404 redirect page

I have an .aspx page as my custom 404 page on a site set up on IIS 7. I need to retrieve the original URL that the user was trying to access in order to do some processing on the 404 page. The trick is that I need to specifically handle 404's that do not contain a .aspx extension (e.g http://example.com/testurl), which do not get routed through ASP.NET's custom errors section. I can configure IIS to point to my custom 404, but at that point I do not know how to get my original URL? Does anyone know if this is possible?
Thanks,
Mike
Yes, it is possible to get the URL that causes the 404 error, you just need to make sure you have IIS configured properly.
There are two cases you need to handle, one is where the error comes from an .aspx or other page that is handled by .NET, and the other is where the error comes from a bad folder (as in your question, http://example.com/testurl) or filename (for example, *.htm) that is not handled by .NET. In IIS 7, you'll need to configure a custom 404 error under ".NET Error Pages" in the "ASP.NET" section for your web app, and also under "Error Pages" in the "IIS" section. The web.config changes end up looking something like this:
<system.web>
<!-- other system.web stuff -->
<customErrors defaultRedirect="/Error404.aspx" mode="On" redirectMode="ResponseRewrite">
<error redirect="/Error404.aspx" statusCode="404" />
</customErrors>
</system.web>
<system.webServer>
<!-- other system.webServer stuff -->
<httpErrors errorMode="Custom">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" prefixLanguageFilePath="" path="/Error404.aspx" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>
Note: the redirectMode="ResponseRewrite" listed above is important if you want your 404 pages to actually return 404 messages and I don't think it can be set through IIS.
In my example, I created a page called Error404.aspx to handle all of the 404 errors. When a .NET page (.aspx, etc) throws a 404 exception, the original filename can be found in the aspxerrorpath querystring variable. When a regular htm or other page causes a 404 error, the original path can be read from the Request.RawUrl property. I used the following code in my Error404.aspx page to handle either case:
public partial class Error404 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
OriginalUrl = Request.QueryString["aspxerrorpath"] ?? Request.RawUrl;
Server.ClearError();
Response.Status = "404 not found";
Response.StatusCode = 404;
}
public string OriginalUrl { get; private set; }
}
By default, the 404 error page will not return a 404 status code, so you need to set it manually. See this post for more detail.

Resources