1

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

ar.gorgin
  • 4,765
  • 12
  • 61
  • 100

1 Answers1

0

This because the exception is being handled by the [HandleError] attribute somewhere in your code (the stack trace or target site of the exception will tell you where.)

So you want to find and remove that attribute. [HandleError] creates 'System.Web.Mvc.HandleErrorInfo' and passes that into \Views\Shared\Error.cshtml as it's model but of course you have changed model for the error page to

@model Behkam.Models.ErrorModel

Hence:

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

Note that a standard MVC app template does the following to globally register the [HandleErrorAttribute]:

public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
           filters.Add(new HandleErrorAttribute());       
        }
    }

So this is the likely culprit.

rism
  • 11,932
  • 16
  • 76
  • 116
  • I added new view for Error in /Error/Index. and send ErrorModel for it. and i don't have any `[HandleError]` attribute in my project. – ar.gorgin Mar 07 '15 at 09:45
  • I'm quite sure you do as that is the only class that generates 'System.Web.Mvc.HandleErrorInfo'. – rism Mar 07 '15 at 09:58