我希望将所有 401 错误重定向到自定义错误页面。我最初在 web.config 中设置了以下条目。
<customErrors defaultRedirect="ErrorPage.aspx" mode="On">
<error statusCode="401" redirect="~/Views/Shared/AccessDenied.aspx" />
</customErrors>
使用 IIS Express 时,我收到库存 IIS Express 401 错误页面。
如果我不使用 IIS Express,则会返回空白页面。使用 Google Chrome 的“网络”选项卡检查响应,我发现当页面为空白时,标题中会返回 401 状态
到目前为止我所尝试的是使用 这个答案,因为我使用的是 IIS Express 但无济于事。我尝试使用
和
的组合,但没有成功 - 仍然显示标准错误或空白页面。
httpErrors
部分目前基于 来自 href="https://stackoverflow.com/questions/2480006/what-is-the-difference- Between-customerrors-and-httperrors">上述问题(我还发现了另一个非常有希望的答案但是没有运气- 空白响应)
<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>
我什至修改了 applicationhost.config
文件并将
修改为
基于来自 iis.net。在我的努力过程中,我还偶然发现了这个错误,如另一个SO问题中所述。
如何在 Asp.Net Mvc 3 中显示自定义错误页面?
其他信息
以下控制器操作已使用特定用户的Authorize
属性进行修饰。
[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");
}
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 Authorize
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");
}
发布评论
评论(4)
我使用以下步骤:
AND:
AND 错误文件夹中的索引视图:
AND - 最后一步:
I use these steps:
AND:
AND the index view in Error folder:
AND -the final step:
我始终无法让 web.config 和 MVC 中的 CustomErrors 能够很好地协同工作,所以我放弃了。我改为这样做。
在 global.asax 中:
在 ErrorsController 中:
在 web.config 中:
无论在何处或如何创建错误,这都对我有用。目前还没有处理 401,但您可以很容易地添加它。
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.
也许我遗漏了一些东西,但 MVC 有一个默认的全局
ErrorHandlerAttribute
使用自定义错误。 此处对此进行了很好的解释。您需要做的就是在配置中打开
自定义错误
,然后设置自定义错误重定向,最好重定向到静态HTML
文件(以防应用程序出现错误) 。如果您愿意,还可以指向自定义控制器来显示错误。在下面的示例中,我刚刚使用了到名为
Error
的Controller
的默认路由,其中包含名为Index
的操作和名为 < code>id(接收错误代码)。您当然可以使用您想要的任何路由。您的示例不起作用,因为您尝试直接链接到Views
目录,而不通过Controller
。 MVC .NET 不直接向Views
文件夹提供请求。ErrorHandlerAttribute 还可以广泛与
Controllers/Actions
一起使用,将错误重定向到与该操作相关的命名Views
控制器。例如,要在发生ArgumentException
类型的异常时显示名为MyArgumentError
的View
,您可以使用:当然,另一个选项是更新 stock <
Shared
中的 code>Error 页面。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
.查看 web.config 的第一部分,您直接指向 .aspx 页面。当我设置错误页面时,我直接指向控制器和操作。例如:
我有一个包含所有必需操作的错误控制器。我认为 MVC 不能很好地直接调用 .aspx 页面。
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.