可以呀,,,我有一個錯誤控制器來亨德爾不同的錯誤。
在您的web.config中,添加以下內容以啓用customErrors模式,並在發生任何錯誤時將其設置爲重定向到您的控制器。
<system.web>
<customErrors mode="On" defaultRedirect="/error/problem">
<error statusCode="404" redirect="/error/notfound" />
<error statusCode="500" redirect="/error/problem" />
</customErrors>
</system.web>
,如果你想更具體的您可以添加更多的錯誤類型上面,沒有列出任何錯誤都將回落到默認的重定向。
這是我的錯誤控制器:
public class ErrorController : Controller
{
public ActionResult NotFound()
{
ErrorViewModel model = BaseViewModelBuilder.CreateViewModel<ErrorViewModel>("We couldn't find what you were looking for");
// Get the URL that caused the 404 error.
model.ErrorUrl = Request.UrlReferrer;
// Set the response status code to 404
Response.StatusCode = (int)HttpStatusCode.NotFound;
Response.TrySkipIisCustomErrors = true;
return View(model);
}
public ActionResult Problem()
{
ErrorViewModel model = BaseViewModelBuilder.CreateViewModel<ErrorViewModel>("Well this is embarrassing...");
return View(model);
}
}
和您可能已經猜到我在錯誤視圖文件夾中的Error.aspx,Problem.aspx意見。