I want to create a custom error page for my MVC4 project with my own model.
I used the approach from Darin Dimitrov from Custom error pages on asp.net MVC3
Now i wanted to use my own (test)model:
public class ErrorModel
{
public string message { get; set; }
}
Now when I use customErrors mode="Off" everything works fine but only on the local machine. When I access the machine from a remote machine however the default error is shown. (which is the intended way - i guess)
Now when i set the System suddenly expects his own model instead of mine:
"The model item passed into the dictionary is of type 'System.Web.Mvc.HandleErrorInfo', but this dictionary requires a model item of type 'Project.Controllers.ErrorModel'.
How can i either force the system to show the page through customErrors="Off" to the remote system when the mode is Off or how to force him to use my model instead of the HandleErrorInfo model?
My ErrorController
public class ErrorController : Controller
{
public ActionResult General(Exception exception)
{
var viewModel = new ErrorModel()
{
message = exception.Message
};
return View(viewModel);
}
The View:
@model SRMv2.WebUI.Controllers.ErrorModel
@{
}
@Html.Raw(Model.message)
And in the Global.asax
protected void Application_Error()
{
HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
HttpContext.Current.Response.Cache.SetValidUntilExpires(false); HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();
var exception = Server.GetLastError();
var httpException = exception as HttpException;
Response.Clear();
Server.ClearError();
var routeData = new RouteData();
routeData.Values["controller"] = "Errors";
routeData.Values["action"] = "General";
routeData.Values["exception"] = exception;
Response.StatusCode = 500;
if (httpException != null)
{
Response.StatusCode = httpException.GetHttpCode();
switch (Response.StatusCode)
{
case 403:
routeData.Values["action"] = "Http403";
break;
case 404:
routeData.Values["action"] = "Http404";
break;
}
}
IController errorController = new ErrorController();
var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
errorController.Execute(rc);
}