Asp.net 自定义应用程序错误-ASP MVC

Asp.net 自定义应用程序错误-ASP MVC,asp.net,asp.net-mvc,vb.net,exception-handling,Asp.net,Asp.net Mvc,Vb.net,Exception Handling,我正在尝试处理一个webapp中的异常,在该应用中,我正在工作并且工作正常,直到我将类似于“的url放入并在visual studio上显示此错误: system.web.dll上发生system.web.httpexception类型的异常,但未在用户代码中处理 我想做什么? 如果页面不存在,则显示404页面 在其他情况下,显示一般错误 Global.asax.vb中的代码 我做错了什么?或者我将如何处理这些错误 提前感谢您提供的信息。我喜欢在web.config中使用httpErrors。

我正在尝试处理一个webapp中的异常,在该应用中,我正在工作并且工作正常,直到我将类似于的url放入并在visual studio上显示此错误:

system.web.dll上发生system.web.httpexception类型的异常,但未在用户代码中处理

我想做什么?

如果页面不存在,则显示404页面

在其他情况下,显示一般错误

Global.asax.vb中的代码

我做错了什么?或者我将如何处理这些错误


提前感谢您提供的信息。

我喜欢在web.config中使用
httpErrors
。这是一个IIS设置,因此您必须将其放入web.config的
system.webserver
部分。因为它是一个IIS级别的设置,所以可以很好地捕获404,尤其是那些与您的任何路由都不匹配的404

以下是我在项目中使用的代码:

<httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="400" subStatusCode="-1" />
  <remove statusCode="403" subStatusCode="-1" />
  <remove statusCode="404" subStatusCode="-1" />
  <remove statusCode="500" subStatusCode="-1" />
  <error statusCode="400" path="/Error/BadRequest" responseMode="ExecuteURL"/>
  <error statusCode="403" path="/Error/NotAuthorized" responseMode="ExecuteURL" />
  <error statusCode="404" path="/Error/NotFound" responseMode="ExecuteURL" />
  <error statusCode="500" path="/Error" responseMode="ExecuteURL" />
</httpErrors>

ex
的值是多少?@mason它的值是{”“}。所有对象都可以在Hello@jpishko中看到它,感谢您的回答和时间,我测试了您的解决方案,没有任何效果。可能是如何在IIS上创建站点的?请确保删除
应用程序\u Error
中的代码,尤其是
服务器.ClearError()
行。还要删除您在web.config中的
customErrors
中定义的任何
error
标记。在
ErrorController
中设置一个断点,以确保在输入一个不存在的URL时,该URL将导致404错误。我更新了答案,加入了我使用的
ErrorController
<httpErrors errorMode="Custom" existingResponse="Replace">
  <remove statusCode="400" subStatusCode="-1" />
  <remove statusCode="403" subStatusCode="-1" />
  <remove statusCode="404" subStatusCode="-1" />
  <remove statusCode="500" subStatusCode="-1" />
  <error statusCode="400" path="/Error/BadRequest" responseMode="ExecuteURL"/>
  <error statusCode="403" path="/Error/NotAuthorized" responseMode="ExecuteURL" />
  <error statusCode="404" path="/Error/NotFound" responseMode="ExecuteURL" />
  <error statusCode="500" path="/Error" responseMode="ExecuteURL" />
</httpErrors>
[AllowAnonymous]
public class ErrorController : Controller
{
    public ActionResult Index()
    {
        Response.StatusCode = 500;
        return View("Error");
    }

    public ViewResult BadRequest()
    {
        Response.StatusCode = 400;
        return View("BadRequest");
    }

    public ViewResult NotFound()
    {
        Response.StatusCode = 404;
        return View("NotFound");
    }

    public ViewResult NotAuthorized()
    {
        Response.StatusCode = 403;
        return View("NotAuthorized");
    }
}