【发布时间】:2015-03-21 03:30:33
【问题描述】:
我有 ASP.NET MVC 5 应用程序,但它是否是 MVC 3、4 并不重要。我安装了启用重定向验证的 NWebSec 3.0 模块。我有 Application_Error 方法(即使我没有,问题也是一样的!):
protected void Application_Error(object sender, EventArgs e)
{
HttpContext httpContext = ((MvcApplication)sender).Context;
Exception exception = Server.GetLastError();
if(exception is NWebsec.Core.Exceptions.RedirectValidationException)
{
Trace.WriteLine("How to redirect from here to ErrorController/Index?");
}
else
{
Trace.WriteLine("How to redirect from here to ErrorController/Index?");
下面是 HomeControllers 中的两种测试方法:
public ActionResult Redirect()
{
return new RedirectResult("http://www.deshow.net/d/file/travel/2010-04/bing-landscape-wallpaper-845-2.jpg");
}
public ActionResult ParseError()
{
int.Parse("test");
return View();
}
我有标准的共享错误视图和
<customErrors mode="On" defaultRedirect="~/Error" />
已启用。
我希望无论出现什么错误,我都会被重定向到错误视图。但当我调用 Redirect 方法时,情况并非如此。不知何故,当重定向错误被抛出时,标准的“自定义错误”机制被绕过,标准错误视图不显示。
另一个细节是,在 ParseError() 被命中之后,Application_Error 没有被命中,并且标准的“自定义错误”视图被显示,但是当 Redirect()被命中时,没有“自定义错误”视图而是黄屏死机。这让我觉得也许我没有正确理解 http 模块的“管道”。
NWebSec 与错误重定向相交并引发 NWebsec.Core.Exceptions.RedirectValidationException。我想是这样的,因为 NwebSec 被注册为一个模块
<system.webServer>
<modules>
...
<add name="NWebsecHttpHeaderSecurityModule" type="NWebsec.Modules.HttpHeaderSecurityModule, NWebsec, Version=3.2.0.0, Culture=neutral, PublicKeyToken=3613da5f958908a1" />
...
</modules>
</system.webServer>
【问题讨论】:
标签: asp.net asp.net-mvc asp.net-mvc-4 asp.net-mvc-5 response.redirect