i create the correct model type and dont understand where this comes from. any ideas?
System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Web.Mvc.HandleErrorInfo', but this dictionary requires a model item of type 'BusinessLogic.Models.ErrorCodeModel'. at System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) at System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) at System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData) at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) at System.Web.Mvc.Controller.ExecuteCore() at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) at System.Web.Mvc.MvcHandler.c__DisplayClass6.c__DisplayClassb.b__5() at System.Web.Mvc.Async.AsyncResultWrapper.c__DisplayClass1.b__0() at System.Web.Mvc.MvcHandler.c__DisplayClasse.b__d() at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
in the global.asax.cs i register a custom attribute:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new Controllers.ExtendendHandleErrorAttribute());
}
the definition looks like this:
public class ExtendendHandleErrorAttribute : HandleErrorAttribute
{
public override void OnException(ExceptionContext filterContext)
{
LogErrors(filterContext);
try
{
base.OnException(filterContext);
var typedResult = filterContext.Result as ViewResult;
if (typedResult != null)
{
var tmpModel = typedResult.ViewData.Model;
typedResult.ViewData = filterContext.Controller.ViewData;
typedResult.ViewData.Model = CreateModel(filterContext);
filterContext.Result = typedResult;
}
}
catch(Exception ex)
{
new LogManager().Log("ExtendendHandleErrorAttribute error", ex);
}
}
interesting here that i create the ErrorCodeModel.
private ErrorCodeModel CreateModel(ExceptionContext filterContext)
{
var model = new ErrorCodeModel();
if (filterContext.HttpContext.Session != null)
{
var session = filterContext.HttpContext.Session;
model.SessionId = session.SessionID;
StateHandler stateHandler = new StateHandler(session);
model.FapiErrorCode = stateHandler.CustomErrorCode.ToString();
try
{
model.GlobalData = new GlobalDataBuilder(stateHandler).Build();
model.ErrorMessage = model.GlobalData.ErrorText.TechnicalError;
}
catch { }
}
return model;
}
my Web.config
<customErrors mode="Off" defaultRedirect="Error">
<error statusCode="404" redirect="Error/FileNotFound" />
</customErrors>
In ~/Views/Shared/Error.cshtml replace the first line:
@model System.Web.Mvc.HandleErrorInfo
with
@model BusinessLogic.Models.ErrorCodeModel
When you use the [HandleError] attribute on a controller or action, any exceptions thrown will cause a redirect to custom error pages as described by your web.config. By default the errors go to the ~\Views\Shared\Error.cshtml using the HandleErrorInfo model.
The web.config section can be something like:
<system.web>
<customErrors mode="On" defaultRedirect="Error.aspx">
<error statusCode="403" redirect="NoAccess.htm"/>
<error statusCode="404" redirect="FileNotFound.htm"/>
</customErrors>
</system.web>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With