ASP.NET MVC4 CustomErrors DefaultRedirect Ignored

后端 未结 6 1918
悲&欢浪女
悲&欢浪女 2021-02-04 07:50

I have an MVC 4 app, using a custom HandleErrorAttribute to handle only custom exceptions. I would like to intercept the default 404 and other non-500 error pages and replace t

6条回答
  •  广开言路
    2021-02-04 08:12

    Create a Controller ErrorController.

     public class ErrorController : Controller
        {
            //
            // GET: /Error/
    
            public ActionResult Index()
            {
                return View();
            }
    }
    

    Create the Index view for the action.

    in Web.config

    
          
    
    

    When you are handling errors in your code/logic

    [HandleError]
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                ViewBag.Message = "Modify this template to jump-start application.";
    
                return View("Index2");
            }
    }
    

    [HandleError] attribute - will redirected to the Error.cshtml page inside shared folder.

提交回复
热议问题