可以呀,,,我有一个错误控制器来亨德尔不同的错误。
在您的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意见。