Reputation: 39453
In a old site, I was changing the way that CustomErrors works by adding redirectMode="ResponseRewrite"
(new in 3.5 SP1):
<customErrors mode="RemoteOnly" defaultRedirect="Error.aspx" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="404.aspx" />
</customErrors>
The thing is: it shows me the generic error page (the one that you get when you don't set customErrors
. If I remove theredirectMode="ResponseRewrite"
part, it works fine.
I'm sure 3.5 SP1 is installed in the server, because I use the same setting on other sites hosted in the same server.
Any ideas?
Upvotes: 77
Views: 71196
Reputation: 883
According to @Amila's post and confirmation and completion of that post, I have same problem, I dig a lot google but had no chance to find the correct answer. The problem is when you are working with ASP.Net Web Application
, whether it's an MVC
or not you can't achieve custom error using the old way with Webform project
.
Here the Option if you are using ASP.Net Web Application
(whether it's an MVC
or not):
In my scenarios I just want to define a custom error for a specific 404 error, The other error defined same as 404 error:
Senario1: Your custom page is a simple HTML
file and placed in the root
:
<configuration>
<system.web>
<customErrors mode="Off" />
</system.web>
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="ErrorPage.html" responseMode="File" />
</httpErrors>
</system.webServer>
</configuration>
aspx
page and placed in the root
:<configuration>
<system.web>
<customErrors mode="Off" />
</system.web>
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="ErrorPage" responseMode="Redirect" />
</httpErrors>
</system.webServer>
</configuration>
Note: I remove the aspx extension due to RouteConfig.cs
in ASP.net application
, you can use ErrorPage.aspx
if you like, it's optional.
aspx
page and placed in the [ex: Page folder in The root (~/Page/ErrorPage.aspx)]
:~/
to the root addressing; So I just addresing without ~/
mark:<configuration>
<system.web>
<customErrors mode="Off" />
</system.web>
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="Page/ErrorPage" responseMode="Redirect" />
</httpErrors>
</system.webServer>
</configuration>
Upvotes: 1
Reputation: 4189
Due to the reliance on Server.Transfer
it seems that the internal implementation of ResponseRewrite
isn't compatible with MVC.
This seems like a glaring functionality hole to me, so I decided to re-implement this feature using a HTTP module, so that it just works. The solution below allows you to handle errors by redirecting to any valid MVC route (including physical files) just as you would do normally.
<customErrors mode="RemoteOnly" redirectMode="ResponseRewrite">
<error statusCode="404" redirect="404.aspx" />
<error statusCode="500" redirect="~/MVCErrorPage" />
</customErrors>
This has been tested on the following platforms;
namespace Foo.Bar.Modules {
/// <summary>
/// Enables support for CustomErrors ResponseRewrite mode in MVC.
/// </summary>
public class ErrorHandler : IHttpModule {
private HttpContext HttpContext { get { return HttpContext.Current; } }
private CustomErrorsSection CustomErrors { get; set; }
public void Init(HttpApplication application) {
System.Configuration.Configuration configuration = WebConfigurationManager.OpenWebConfiguration("~");
CustomErrors = (CustomErrorsSection)configuration.GetSection("system.web/customErrors");
application.EndRequest += Application_EndRequest;
}
protected void Application_EndRequest(object sender, EventArgs e) {
// only handle rewrite mode, ignore redirect configuration (if it ain't broke don't re-implement it)
if (CustomErrors.RedirectMode == CustomErrorsRedirectMode.ResponseRewrite && HttpContext.IsCustomErrorEnabled) {
int statusCode = HttpContext.Response.StatusCode;
// if this request has thrown an exception then find the real status code
Exception exception = HttpContext.Error;
if (exception != null) {
// set default error status code for application exceptions
statusCode = (int)HttpStatusCode.InternalServerError;
}
HttpException httpException = exception as HttpException;
if (httpException != null) {
statusCode = httpException.GetHttpCode();
}
if ((HttpStatusCode)statusCode != HttpStatusCode.OK) {
Dictionary<int, string> errorPaths = new Dictionary<int, string>();
foreach (CustomError error in CustomErrors.Errors) {
errorPaths.Add(error.StatusCode, error.Redirect);
}
// find a custom error path for this status code
if (errorPaths.Keys.Contains(statusCode)) {
string url = errorPaths[statusCode];
// avoid circular redirects
if (!HttpContext.Request.Url.AbsolutePath.Equals(VirtualPathUtility.ToAbsolute(url))) {
HttpContext.Response.Clear();
HttpContext.Response.TrySkipIisCustomErrors = true;
HttpContext.Server.ClearError();
// do the redirect here
if (HttpRuntime.UsingIntegratedPipeline) {
HttpContext.Server.TransferRequest(url, true);
}
else {
HttpContext.RewritePath(url, false);
IHttpHandler httpHandler = new MvcHttpHandler();
httpHandler.ProcessRequest(HttpContext);
}
// return the original status code to the client
// (this won't work in integrated pipleline mode)
HttpContext.Response.StatusCode = statusCode;
}
}
}
}
}
public void Dispose() {
}
}
}
Usage
Include this as the final HTTP module in your web.config
<system.web>
<httpModules>
<add name="ErrorHandler" type="Foo.Bar.Modules.ErrorHandler" />
</httpModules>
</system.web>
<!-- IIS7+ -->
<system.webServer>
<modules>
<add name="ErrorHandler" type="Foo.Bar.Modules.ErrorHandler" />
</modules>
</system.webServer>
Upvotes: 17
Reputation: 13138
I built an error page in aspx that transfers the query to an ASP.NET MVC controller. You can rewrite the query to this aspx page and it will transfer the query to your custom controller.
protected void Page_Load(object sender, EventArgs e)
{
//Get status code
var queryStatusCode = Request.QueryString.Get("code");
int statusCode;
if (!int.TryParse(queryStatusCode, out statusCode))
{
var lastError = Server.GetLastError();
HttpException ex = lastError as HttpException;
statusCode = ex == null ? 500 : ex.GetHttpCode();
}
Response.StatusCode = statusCode;
// Execute a route
RouteData routeData = new RouteData();
string controllerName = Request.QueryString.Get("controller") ?? "Errors";
routeData.Values.Add("controller", controllerName);
routeData.Values.Add("action", Request.QueryString.Get("action") ?? "Index");
var requestContext = new RequestContext(new HttpContextWrapper(Context), routeData);
IController controller = ControllerBuilder.Current.GetControllerFactory().CreateController(requestContext, controllerName);
controller.Execute(requestContext);
}
Find more details here : https://stackoverflow.com/a/27354140/143503
Upvotes: 1
Reputation: 2819
The only way that worked perfectly for me is to turn off custom errors and replace iis's error pages via web.config. It sends the correct status code with the response and has the benefit of not going through the mvc.
here's the code
Turn off custom errors
<customErrors mode="Off" />
Replace error pages
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="404" path="Error404.html" responseMode="File" />
<error statusCode="500" path="Error.html" responseMode="File" />
</httpErrors>
Note. Use responsemode="file"
if the url is a direct link to a file
info : http://tipila.com/tips/use-custom-error-pages-aspnet-mvc
Upvotes: 56
Reputation: 868
I have found out that if you use redirectMode="ResponseRewrite" then you need to add something in the rewrite area of the web.config file. Problem is when your site is broken! You can't URL rewrite as your site can't call the "virtual.aspx" that handles your rewrite!
Upvotes: 0
Reputation: 1413
It is important to note for anyone trying to do this in an MVC application that ResponseRewrite
uses Server.Transfer
behind the scenes. Therefore, the defaultRedirect
must correspond to a legitimate file on the file system. Apparently, Server.Transfer
is not compatible with MVC routes, therefore, if your error page is served by a controller action, Server.Transfer
is going to look for /Error/Whatever, not find it on the file system, and return a generic 404 error page!
Upvotes: 105
Reputation: 1366
In my particular case, my error page had a master page that had a user control that tried to use Session. If Session isn't available, you get an HttpException: "Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive." Easiest fix is to switch to static html, second easiest fix is to use a simpler error page, hardest fix is to make incredibly sure that your error page makes no assumptions anywhere (like that Session won't throw an exception, for example) and can't possibly error out.
Upvotes: 0
Reputation: 12021
What's happening is IIS is seing the error status code and presenting it's own error page instead of yours. To solve you need to set this in the code behind page of your error page to prevent IIS from doing this:
Response.TrySkipIisCustomErrors = true;
This will only work in IIS7 or above, for earlier versions of IIS you'll need to play with the error page settings.
Upvotes: 20
Reputation: 224
I know this question is a bit old, but I thought I should point out that it doesn't need to be a static file to get this working.
I ran into a similar thing, and it's just a matter of finding that error in your Error.aspx, in our case it was because the masterpage in use relied on a piece of session data and when ResponseRewrite was set the session is not available to our Error.aspx page.
I haven't worked out yet whether this unavailability of session is due to our specific app config or a "by design" part of ASP.net.
Upvotes: 10
Reputation: 39453
I found that the problem was in Error.aspx. Still can't find what was the actual error in error.aspx that causes the problem.
Changing the page to a static html file solved the problem.
Upvotes: 1