In site.master view, I have some inline code like:
<%=(ViewData["Name"] as Model).Name %>
If ViewData["Name"] is null, the code above will cause exception. Then I want to show the customized error view to user. What I did is set error mapping is web.config like:
<customErrors mode="On" defaultRedirect="~/Error/Unknown">
<error statusCode="404" redirect="~/Error/NotFound" />
<error statusCode="403" redirect="~/Error/NoAccess" />
</customErrors>
My Error controller is:
public class ErrorController : Controller
{
public ActionResult Index()
{
return View();
}
[AcceptVerbs(HttpVerbs.Get)]
public ViewResult NotFound(string aspxerrorpath)
{
this.ViewData["HttpError::AspxErrorPath"] = aspxerrorpath;
this.Response.StatusCode = (int)HttpStatusCode.NotFound;
return this.View("NotFound");
}
[AcceptVerbs(HttpVerbs.Get)]
public ViewResult NoAccess()
{
Response.StatusCode = (int)HttpStatusCode.Unauthorized;
return View("NoAccess");
}
[AcceptVerbs(HttpVerbs.Get)]
public ViewResult Unknown()
{
Response.StatusCode = (int)HttpStatusCode.InternalServerError;
return View("Unknown");
}
}
But I can't captch the error. I will get error in borwoser like
Server Error in '/' Application. Runtime Error ...
It means the error was not captured. How to resolve this problem?