I want all 401 errors to be be redirected to a custom error page. I have initially setup the following entry in my web.config.
<customErrors defaultRedirect="ErrorPage.aspx" mode="On">
<error statusCode="401" redirect="~/Views/Shared/AccessDenied.aspx" />
</customErrors>
When using IIS Express I receive the stock IIS Express 401 error page.
In the event when I do not use IIS Express a blank page is returned. Using Google Chrome's Network tab to inspect the response, I see that while the page is blank a 401 status is returned in the headers
What I have tried thus far is using suggestions from this SO answer since I am using IIS Express but to no avail. I have tried using a combination <custom errors>
and <httpErrors>
with no luck - the standard error or blank page is still displayed.
The httpErrors
section looks like this at the moment based on the link from the above SO question ( I also found another very promising answer however no luck - blank response)
<system.webServer>
<httpErrors errorMode="DetailedLocalOnly" existingResponse="PassThrough" >
<remove statusCode="401" />
<error statusCode="401" path="/Views/Shared/AccessDenied.htm" />
</httpErrors>
<!--
<httpErrors errorMode="Custom"
existingResponse="PassThrough"
defaultResponseMode="ExecuteURL">
<remove statusCode="401" />
<error statusCode="401" path="~/Views/Shared/AccessDenied.htm"
responseMode="File" />
</httpErrors>
-->
</system.webServer>
I have even modified the applicationhost.config
file and modified <httpErrors lockAttributes="allowAbsolutePathsWhenDelegated,defaultPath">
to <httpErrors lockAttributes="allowAbsolutePathsWhenDelegated">
based on information from iis.net. During the course of my endeavours I also managed to stumbled upon this error as described in another SO question.
How do I display custom error pages in Asp.Net Mvc 3?
Additional info
The following controller actions have been decorated with the Authorise
attribute for a specific user.
[HttpGet]
[Authorize(Users = "domain\\userXYZ")]
public ActionResult Edit()
{
return GetSettings();
}
[HttpPost]
[Authorize(Users = "domain\\userXYZ")]
public ActionResult Edit(ConfigurationModel model, IList<Shift> shifts)
{
var temp = model;
model.ConfiguredShifts = shifts;
EsgConsole config = new EsgConsole();
config.UpdateConfiguration(model.ToDictionary());
return RedirectToAction("Index");
}
Looking at the first part of your web.config there, you're pointing to an .aspx page directly. When I setup my error pages I pointed directly to a controller and action. For example:
And I had an Error controller with all the required actions. I don't think MVC plays well with direct calls to .aspx pages.
I was never able to get CustomErrors in a web.config and MVC to play nice together, so I gave up. I do this instead.
In global.asax:
In ErrorsController:
In web.config:
That's worked for me no matter where or how the error is created. 401 isn't handled there right now but you could add it pretty easily.
I use these steps:
AND:
AND the index view in Error folder:
AND -the final step:
Maybe I'm missing something, but MVC has a default global
ErrorHandlerAttribute
that uses custom errors. This is explained quite well here.All you need to do is turn on
custom errors
in the config, and then setup custom error redirects, preferably to a staticHTML
file (in case there's errors with the app).If you prefer could also point to a custom
Controller
to display the errors. In the following example I've just used the default routing to aController
namedError
, with an action calledIndex
, and string parameter namedid
(to receive the errorcode). You could of course use any routing you desire. Your example isn't working because you are trying to link directly into theViews
directory without going via aController
. MVC .NET doesn't serve requests to theViews
folder directly.The ErrorHandlerAttribute can also be used extensively with
Controllers/Actions
to redirect errors to namedViews
related to theController
. For example to show theView
namedMyArgumentError
when a exception of typeArgumentException
occurs you could use:Of course another option is to update the stock
Error
page inShared
.