方式一:使用web.config配置
[HandleError]
public class MvcHandleController : Controller
{
public ActionResult Index()
{
return View();
}
}
web.config配置如下
<customErrors mode="On" defaultRedirect="~/Account/Error">
<error statusCode="403" redirect="~/404.html"/>
<error statusCode="404" redirect="~/500.html"/>
</customErrors>
方式二:
Controller
public class MvcHandleController : Controller
{
[HandleError(View = "Error")]
public ActionResult Index()
{
return View();
}
}
view
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<HandleErrorInfo>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
AboutError
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
通过强类型获取异常信息
<%= Model.Exception.Message %>
</asp:Content>