找不到自定义错误页面

发布于 2024-09-13 09:35:39 字数 871 浏览 8 评论 0原文

这是我如何定义它的(在本地,在我的开发计算机上):

    <customErrors mode="On" defaultRedirect="Error.aspx">
        <error statusCode="404" redirect="NotFound.aspx" />
    </customErrors>

我有 [HandleError] 属性:

[Authorize]
[HandleError]
public class HomeController : Controller
{ // etc.

然而,当我输入 http://localhost:1986/blah,我收到以下错误:

找不到资源。 描述:HTTP 404。您正在查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请检查以下 URL 并确保拼写正确。

请求的 URL:/NotFound.aspx

它尝试访问的 URL 正如您所期望的: http://localhost:1986/NotFound.aspx?aspxerrorpath=/blah

所以它正在尝试访问自定义错误文件——但是找不到它。我的共享目录中确实有 NotFound.aspx —— 与 Microsoft 默认提供的 Error.aspx 位于同一位置。为什么找不到呢?

Here's how I have it defined (locally, on my development machine):

    <customErrors mode="On" defaultRedirect="Error.aspx">
        <error statusCode="404" redirect="NotFound.aspx" />
    </customErrors>

And I have the [HandleError] attribute:

[Authorize]
[HandleError]
public class HomeController : Controller
{ // etc.

Yet when I type in http://localhost:1986/blah, I get the following error:

The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /NotFound.aspx

The URL it's trying to go to is as you would expect:
http://localhost:1986/NotFound.aspx?aspxerrorpath=/blah

So it IS attempting to go to the custom error file -- however it can't find it. I do have NotFound.aspx in the Shared directory -- same place as the Error.aspx supplied by Microsoft as a default. Why can't it find it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可爱暴击 2024-09-20 09:35:39

如果 Error.aspx 和 NotFound.aspx 位于共享目录中,是否有控制器为它们提供服务?如果您没有配置某种控制器路由来提供文件,那么它们位于共享文件夹中的事实就无关紧要了。

您有几个选择,您可以创建一个 ErrorController 来处理这些视图的请求并定义指向这些控制器操作的路由:

[OutputCache(CacheProfile = "Default", VaryByParam = "none")]
public class ErrorController : DefaultAreaBaseController
{
    public ViewResult ServiceUnavailable() {
        Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable;

        return View("ServiceUnavailable");
    }

    public ViewResult ServerError() {
        Response.StatusCode = (int)HttpStatusCode.InternalServerError;

        return View("ServerError");
    }

    public new ViewResult NotFound() {
        Response.StatusCode = (int)HttpStatusCode.NotFound;

        return View("NotFound");
    }
}

或者作为替代方案,您可以创建指向物理文件的忽略路由并将错误页面放置在其他位置比 Views 文件夹(如您的根目录):

routes.IgnoreRoute("Error.aspx/{*pathInfo}");
routes.IgnoreRoute("NotFound.aspx/{*pathInfo}");

这些解决方案中的任何一个都是可行的,但是根据您的配置,使用 IgnoreRoute() 可能更理想,因为它将放弃将请求传输到 MVC 的需要,仅提供静态错误页面。

If the Error.aspx and NotFound.aspx are in the shared directory is there a controller wired to served them? If you do not have some sort of controller route configured to serve the files then the fact that they are in the shared folder is irrelevant.

You have a few options, you could create an ErrorController which will handle the requests for those views and define routes pointing to those controller actions:

[OutputCache(CacheProfile = "Default", VaryByParam = "none")]
public class ErrorController : DefaultAreaBaseController
{
    public ViewResult ServiceUnavailable() {
        Response.StatusCode = (int)HttpStatusCode.ServiceUnavailable;

        return View("ServiceUnavailable");
    }

    public ViewResult ServerError() {
        Response.StatusCode = (int)HttpStatusCode.InternalServerError;

        return View("ServerError");
    }

    public new ViewResult NotFound() {
        Response.StatusCode = (int)HttpStatusCode.NotFound;

        return View("NotFound");
    }
}

Or as an alternative, you can create ignore routes pointing at the physical files and place the error pages somewhere other than the Views folder (like your root directory):

routes.IgnoreRoute("Error.aspx/{*pathInfo}");
routes.IgnoreRoute("NotFound.aspx/{*pathInfo}");

Either of these solutions is viable however depending on your configuration using an IgnoreRoute() may be more ideal as it will forgo the need to pipe the request to MVC only to serve a static error page.

天煞孤星 2024-09-20 09:35:39

选项一:

构建一个带有“NotFound”视图和“Unknown”视图的错误控制器。这将接收 500 服务器错误或 404 NotFound 错误并将您重定向到适当的 URL。我并不完全喜欢这个解决方案,因为访问者总是被重定向到错误页面。

http://example.com/Error/Unknown

<customErrors mode="On" defaultRedirect="Error/Unknown">
    <error statusCode="404" redirect="Error/NotFound" />
    <error statusCode="500" redirect="Error/Unknown" />
</customErrors>
  • wwwroot/
    • 控制器
      • 错误.cs
    • 观看次数/
      • 错误/
      • NotFound.aspx
      • 未知.aspx

选项二:

绝对不喜欢这种方法(因为它基本上恢复到 Web 表单,第二个选项是简单地使用静态Error.aspx 页面并忽略 MVC 中的路由),但它仍然有效。您在这里所做的是忽略“静态”目录,将物理错误页面放在其中,并绕过 MVC。

routes.IgnoreRoute("/Static/{*pathInfo}");  //This will ignore everything in the "Static" directory
  • wwwroot/
    • 控制器/
    • 静态/
      • 错误.aspx
    • 观看次数/

选项三:

第三个选项(这是我最喜欢的)是从捕获错误的任何视图返回错误视图。这需要您为“已知”错误编写 Try/Catch 块,然后您可以使用 HandleError 来处理可能出现的未知错误。这将保留最初请求的 URL,但返回 ERROR 视图。

示例:
http://example.com/Products/1234 将显示 ProductID 1234 的详细信息页面
http://example.com/Products/9999 将显示 NotFound错误页面,因为 ProductID 9999 不存在
http://example.com/Errors/NotFound “不应该”显示,因为您处理了这些错误单独在您的控制器中。

Web.Config

<customErrors mode="On">
</customErrors>

控制器

// Use as many or as few of these as you need
[HandleError(ExceptionType = typeof(SqlException), View = "SqlError")]
[HandleError(ExceptionType = typeof(NullReferenceException), View = "NullError")]
[HandleError(ExceptionType = typeof(SecurityException), View = "SecurityError")]
[HandleError(ExceptionType = typeof(ResourceNotFoundException), View = "NotFound")]
Public Class ProductController: Controller{
    public ViewResult Item(string itemID)
    {
        try
        {
            Item item = ItemRepository.GetItem(itemID);
            return View(item);
        }
        catch()
        {
            return View("NotFound");
        }
    }
}

文件夹结构

  • wwwroot/
    • 控制器/
    • 共享/
      • NotFound.aspx
      • NullError.aspx
      • 安全错误.aspx
      • SqlError.aspx
    • 观看次数/

选项四:

最后一个选项是您为 ResourceNotFoundException 之类的内容构建自己的自定义过滤器并将其附加到您的控制器班级。这将执行与上面完全相同的操作,但还有一个额外的好处,即还将错误代码发送到客户端。

理查德·丁沃尔谈论它在他的博客上

Option One:

is to build an Errors Controller with a "NotFound" view along with a "Unknown" view. This will take anything that is a 500 Server error or a 404 NotFound error and redirect you to the appropriate URL. I don't totally love this solution as the visitor is always redirected to an error page.

http://example.com/Error/Unknown

<customErrors mode="On" defaultRedirect="Error/Unknown">
    <error statusCode="404" redirect="Error/NotFound" />
    <error statusCode="500" redirect="Error/Unknown" />
</customErrors>
  • wwwroot/
    • Controllers
      • Error.cs
    • Views/
      • Error/
      • NotFound.aspx
      • Unknown.aspx

Option Two:

I Definitely don't prefer this method (as it is basically reverting back to web forms, The second option is to simply have a static Error.aspx page and ignore the route in MVC), but it works none the less. What you're doing here is ignoring a "Static" directory, placing your physical Error pages in there, and skirting around MVC.

routes.IgnoreRoute("/Static/{*pathInfo}");  //This will ignore everything in the "Static" directory
  • wwwroot/
    • Controllers/
    • Static/
      • Error.aspx
    • Views/

Option Three:

The third option (THIS IS MY FAVORITE) is to return an Error View from whatever view is catching the error. This would require you to code up Try/Catch blocks along the way for "known" errors and then you can use HandleError for the unknown errors that might creep up. What this will do is preserve the originally requested URL but return the ERROR view.

EXAMPLE:
http://example.com/Products/1234 will show a details page for ProductID 1234
http://example.com/Products/9999 will show a NotFound error page because ProductID 9999 doesn't exist
http://example.com/Errors/NotFound "should" never be shown because you handle those errors individually in your controllers.

Web.Config

<customErrors mode="On">
</customErrors>

Controller

// Use as many or as few of these as you need
[HandleError(ExceptionType = typeof(SqlException), View = "SqlError")]
[HandleError(ExceptionType = typeof(NullReferenceException), View = "NullError")]
[HandleError(ExceptionType = typeof(SecurityException), View = "SecurityError")]
[HandleError(ExceptionType = typeof(ResourceNotFoundException), View = "NotFound")]
Public Class ProductController: Controller{
    public ViewResult Item(string itemID)
    {
        try
        {
            Item item = ItemRepository.GetItem(itemID);
            return View(item);
        }
        catch()
        {
            return View("NotFound");
        }
    }
}

Folder Structure

  • wwwroot/
    • Controllers/
    • Shared/
      • NotFound.aspx
      • NullError.aspx
      • SecurityError.aspx
      • SqlError.aspx
    • Views/

Option Four:

The last option would be that you build your own custom filter for things like ResourceNotFoundException and attach it to your controller class. This will do the exact same thing as above but with the added benefit of sending the error code down the line to the client as well.

Richard Dingwall talks about it on his blog.

听你说爱我 2024-09-20 09:35:39

您可以在这里混合 Web 表单和 MVC 概念。在 web.config 中关闭自定义错误。然后在 HandleError 属性中可选地指定类型和视图,默认情况下,在views\CurrentController中搜索error.aspx,然后在views\shared中搜索。虽然您可以使用 HandleError 过滤器进行 404 处理,但您可能只想创建一个仅用于 404 处理的过滤器,此处详细解释了原因和操作方法:

http://richarddingwall.name/2008/08/17/strategies-for-resource-based -404-aspnet-mvc中的错误/

Your mixing web forms and MVC concepts here. Turn custom errors off in the web.config. Then in the HandleError attribute optionally specify the type and view, by default error.aspx is searched for in views\CurrentController then views\shared. Whilst you can get 404 handling working with the HandleError filter you probably want to create a filter just for 404 handling, the reasons and how to are explained in detail here:

http://richarddingwall.name/2008/08/17/strategies-for-resource-based-404-errors-in-aspnet-mvc/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文