第三个文件夹后MVC 5错误处理不工作 [英] mvc 5 error handling not working after third folder

查看:124
本文介绍了第三个文件夹后MVC 5错误处理不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC应用程序5,并与的web.config 定义的自定义错误:

I have an MVC 5 app and with custom errors defined in web.config:

<customErrors mode="RemoteOnly" redirectMode="ResponseRedirect" defaultRedirect="~/Errors/GeneralError">
  <error statusCode="404" redirect="~/Errors/NotFound" />
  <error statusCode="500" redirect="~/Errors/ServerError" />
</customErrors>

如果我输入一个没有存在的网址就好了,只要它是3个级别/文件夹最多将处理它。我也有安装ELMAH,它会记录错误。在另一方面,如果我尝试访问没有存在的网址有4个以上的水平/文件夹,错误将不会被处理。它不会重定向到自定义错误页,或者默认的错误页面。相反,它会返回一个空白页了404头。意思是:

if I type a none existing url it will handle it just fine as long as it is 3 levels/folders maximum. I also have Elmah installed and it will log the error. On the other hand, if I try accessing a none existing url with 4 or more levels/folders, the error will not be handled. It won't redirect to the custom error page, or the default error page. Instead, it will return a blank page with a 404 header. meaning:


  • website.com/blahblah

  • website.com/a/blahblah

  • website.com/a/b/blahblah

  • website.com/blahblah
  • website.com/a/blahblah
  • website.com/a/b/blahblah

将被处理,但 website.com/a/b/c/blahblah 及以上不会。另外,我有一些网页有4个或更多的文件夹,它们做工精细。当地生产的计算机上,并在Azure上发布的网站上会出现此问题。任何人有原因的见解?

will be handled, but website.com/a/b/c/blahblah and above won't. Additionally, I have some pages with 4 or more folders, and they work fine. This problem occurs on local production computer and on the published site on Azure. Anyone has an insight of why?

由于@Nicholas巴顿的链接我做这些chages:

Thanks to @Nicholas Patton's link I made these chages:

(1)我完全删除的customErrors

(1) I removed customErrors completely

(2)添加到了的web.config

<system.webServer>
  <httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="404"/>
  <error statusCode="404" path="/Errors/NotFound" responseMode="ExecuteURL" />
  <remove statusCode="403"/>
  <error statusCode="403" path="/Errors/NotFound" responseMode="ExecuteURL" />
  <remove statusCode="500"/>
  <error statusCode="500" path="/Errors/ServerError" responseMode="ExecuteURL" />
</httpErrors>


(3)违背了文章的建议用我用MVC常规动作的.aspx 文件。例如,在控制器错误我用 NOTFOUND 像这样的动作:


(3) In contrary to the article's suggestion using .aspx files I used regular actions in MVC. For example, in controller Error I used NotFound action like so:

[Route("Errors/NotFound")]
public ActionResult NotFound()
{
    Response.StatusCode = 404;
    return View("Error404");
}

有一些区别了。 404页将不会被重定向到错误/ NOTFOUND?aspxerrorpath = / errorURL / A / B / C 它只会显示在不进行重新定向原始链接自定义错误页。如果您仍然希望 aspxerrorpath 值,你可以简单地获取URL在这样的操作:

There are a few differences now. the 404 page will not be redirect to Error/NotFound?aspxerrorpath=/errorURL/a/b/c it will simply show the custom error page in the original link without redirection. If you still want the aspxerrorpath value you can simply get the URL in the action like this:

[Route("Errors/NotFound")]
public ActionResult NotFound()
{
    string aspxerrorpath = Request.RawUrl;
    // do whatever with aspxerrorpath 

    Response.StatusCode = 404;
    return View("Error404");
}

尽管如此,仍然ELMAH不会记录上述3个文件夹404错误。但是,这是一个新的问题,一个不同的问题。

Nonetheless, Elmah still doesn't log 404 errors above 3 folders. But this is a different issue for a new question.

推荐答案

阅读:
的https://dusted.$c$cs /玄机-ASPNET-MVC-5-错误页面和错误日志记录

从上面的链接:

...如果有人浏览到这不是由ASP.NET处理的网址是什么?
  例如尝试导航到http:// {您-网站} / A / B / C / D / E / F / G。该
  路线没有映射到ASP.NET,因此的Application_Error
  事件将不会引发。

...what if someone navigates to a URL which isn't handled by ASP.NET? For example try navigating to http://{your-website}/a/b/c/d/e/f/g. The route is not mapped to ASP.NET and therefore the Application_Error event will not be raised.

这是一个真正伟大的教程,如果你真的想你的自定义错误页是结实。 :)

This is a really great tutorial if you really want your custom error pages to be beefy. :)

这篇关于第三个文件夹后MVC 5错误处理不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆