很無聊的記錄下。首先IIS 7以上的版本測試才成功html
首先web
//全局ErrorHandler public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); filters.Add(new VcodeFilterAttribute()); }
而後MVC web.config 設置以下測試
<!-- 放到system.webServer --> <httpErrors errorMode="Custom" existingResponse="Replace"> <remove statusCode="404"/> <remove statusCode="500"/> <error statusCode="404" path="/Error/Error404" responseMode="ExecuteURL"/> <error statusCode="500" path="/Error/Error500" responseMode="ExecuteURL"/> </httpErrors>
確保它是關閉的。spa
<customErrors mode="Off" />
而後有個ErrorController。日誌
public class ErrorController : Controller { public ActionResult Error404() { return View(); } public ActionResult Error500() { return View(); } }
View 中,喜歡怎麼寫就怎麼寫。固然要設置一下StatusCode,不然所有返回200code
@{ Response.StatusCode = 404;}
固然也能夠在controller裏面設置htm
日誌能夠寫在global裏面blog
protected void Application_Error(object sender, EventArgs e) { var exception = Server.GetLastError(); //log exception }