ASP.Net MVC HandleError引发500内部服务器错误

时间:2022-11-24 08:31:42

I have the following HandleError filter on my controller:

我的控制器上有以下HandleError过滤器:

[HandleError(ExceptionType = typeof(ArgumentException), View = "DestinationError")]

I've set-up the Web.Config so that customErrors are on. The problem I'm having is that the HandleError filter is working fine, when I run the app locally out of Visual Studio, but when I deploy it to the server all I get is a 500 Internal Server Error, indicating the Error view cannot be found.

我已经设置了Web.Config,以便启用customErrors。我遇到的问题是HandleError过滤器工作正常,当我从Visual Studio本地运行应用程序时,但当我将它部署到服务器时,我得到的是500内部服务器错误,表明错误视图不能是找到。

Has anyone come across this before, I'm suspicious that routing may be the root cause of the problem (hoho). The site gets deployed into a directory in the web root, rather than into the wwwroot itself, so perhaps IIS cannot locate the error file.

有没有人遇到过这个,我怀疑路由可能是问题的根本原因(hoho)。该站点部署到Web根目录中的目录中,而不是部署到wwwroot本身,因此IIS可能无法找到错误文件。

4 个解决方案

#1


3  

To answer my own question the magic is to turn off HTTP Errors in IIS. I'm not delighted in this workaround, so if anyone has any better ideas, I'd love to hear them.

要回答我自己的问题,神奇的是关闭IIS中的HTTP错误。我对这种解决方法并不满意,所以如果有人有更好的想法,我很乐意听到它们。

#2


3  

Otherwise you can use the Web.Config configuration and set it to the expected controller's actions. Like this:

否则,您可以使用Web.Config配置并将其设置为预期控制器的操作。像这样:

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

Then imagine you have an Error controlller (/Error) which points out to an index action

然后假设你有一个错误控制器(/ Error),它指出了一个索引操作

public class ErrorController : Controller
{
    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Index()
    {
        Response.StatusCode = (int)HttpStatusCode.InternalServerError;
        return View("Index");
    }

    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult NotFound()
    {
        Response.StatusCode = (int)HttpStatusCode.NotFound;
        return View("NotFound");
    }
}

#3


0  

What if you try the following?

如果你尝试以下怎么办?

Response.TrySkipIisCustomErrors = true;

#4


0  

I had the same problem after migrating to MVC 3 RC. Managed to get around it by adding the layout / master page.

迁移到MVC 3 RC后我遇到了同样的问题。通过添加布局/母版页来管理它。

@inherits System.Web.Mvc.WebViewPage<System.Web.Mvc.HandleErrorInfo>

@{
    View.Title = "Error";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

Now the internal server error is gone, but I think it's a bug somewhere.

现在内部服务器错误消失了,但我认为这是一个错误。

#1


3  

To answer my own question the magic is to turn off HTTP Errors in IIS. I'm not delighted in this workaround, so if anyone has any better ideas, I'd love to hear them.

要回答我自己的问题,神奇的是关闭IIS中的HTTP错误。我对这种解决方法并不满意,所以如果有人有更好的想法,我很乐意听到它们。

#2


3  

Otherwise you can use the Web.Config configuration and set it to the expected controller's actions. Like this:

否则,您可以使用Web.Config配置并将其设置为预期控制器的操作。像这样:

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

Then imagine you have an Error controlller (/Error) which points out to an index action

然后假设你有一个错误控制器(/ Error),它指出了一个索引操作

public class ErrorController : Controller
{
    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Index()
    {
        Response.StatusCode = (int)HttpStatusCode.InternalServerError;
        return View("Index");
    }

    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult NotFound()
    {
        Response.StatusCode = (int)HttpStatusCode.NotFound;
        return View("NotFound");
    }
}

#3


0  

What if you try the following?

如果你尝试以下怎么办?

Response.TrySkipIisCustomErrors = true;

#4


0  

I had the same problem after migrating to MVC 3 RC. Managed to get around it by adding the layout / master page.

迁移到MVC 3 RC后我遇到了同样的问题。通过添加布局/母版页来管理它。

@inherits System.Web.Mvc.WebViewPage<System.Web.Mvc.HandleErrorInfo>

@{
    View.Title = "Error";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

Now the internal server error is gone, but I think it's a bug somewhere.

现在内部服务器错误消失了,但我认为这是一个错误。