I want to created a Cutsum error in mvc .
I add custom error in web.config
<customErrors mode="On" defaultRedirect="~/Error">
<error redirect="~/Error/NotFound" statusCode="404" />
</customErrors>
in global
protected void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
Server.ClearError();
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "Index");
routeData.Values.Add("exception", exception);
if (exception.GetType() == typeof(HttpException))
{
routeData.Values.Add("statusCode", ((HttpException)exception).GetHttpCode());
}
else
{
routeData.Values.Add("statusCode", 500);
}
IController controller = new ErrorController();
controller.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
Response.End();
}
and in controller
public class ErrorController : Controller
{
//
// GET: /Error/
public ActionResult Index(int statusCode, Exception exception)
{
Response.StatusCode = statusCode;
var model = new ErrorModel() {Exception = exception, HttpStatusCode = statusCode};
return View(model);
}
public ActionResult NotFound()
{
Response.StatusCode = 404; //you may want to set this to 200
return View();
}
}
and in view
@model Behkam.Models.ErrorModel
@if (Model != null && HttpContext.Current.IsDebuggingEnabled)
{
<i class="icon-404"></i>
<h1>@Model.HttpStatusCode</h1>
<h2>خطا در دریافت اطلاعات ! </h2>
<p class="page-404">@Model.Exception.Message .@Html.ActionLink("بازگشت به کارتابل ", "index", "Dashboard")</p>
}
when i get error in a page, it return error for all exception:
The model item passed into the dictionary is of type 'System.Web.Mvc.HandleErrorInfo', but this dictionary requires a model item of type 'Behkam.Models.ErrorModel