tags:

views:

113

answers:

1

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?

+1  A: 

Custom error pages in ASP.Net MVC are handled by the [HandleError] action filter attribute. For more information on how to use it, see ScottGu or Danny Tuppenny's blog posts about it.

This question might also relate to your problem.

Jacob
Thanks. I put [HandleError] for all controller class. It suppose that only when the running goes into the controller. But Site.Master is before any controller. It seems there is no controller take the responsibility for site.master view. So there is no way to put [HandleError] for site.master. Any idea?
KentZhou