Reputation: 12862
I have a relatively untouched MVC4 project with the following in my Web.Release.config:
<system.web>
<customErrors mode="On" defaultRedirect="~/Error">
<error redirect="~/Error/NotFound" statusCode="404" />
</customErrors>
</system.web>
It's not working though - I get normal error pages when in Release mode.
If I place that code in my Web.Config, it works as expected. I only want this applied when in Release though.
I also tried this in web.release.config:
<system.web>
<customErrors mode="On" defaultRedirect="~/Error" xdt:Transform="Replace">
<error redirect="~/Error/NotFound" statusCode="404" />
</customErrors>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
To no avail.
Why would this be happening?
UPDATE: If I use the following in my global.asax:
void Application_Error(object sender, EventArgs e)
{
#if DEBUG
#else
Response.Redirect("/Error");
#endif
return;
}
I get the desired behavior. I feel like I should be able to use the web.config settings only though... so I'd like to leave this open.
Upvotes: 2
Views: 4465
Reputation: 5750
The problem is you're not publishing. The web.release.config file is only transformed during publish as far as I know. If you're just building and running locally that file won't be used.
Also in your Application_Error
you should get the status code for the error. Something like
var exception = Server.GetLastError();
var httpException = exception as HttpException;
if (httpException != null)
{
if (httpException.GetHttpCode() == 404)
RedirectToMyNotFoundPage();
}
Otherwise you will basically just handle all errors, which may or may not be what you want to do.
If you want to handle MvcErrors, and get the controller/action that the exception occured in, you should look into Filters.
Here's a good article on error filtering if that's what you're going for.
Upvotes: 4