如何在MVC中获取错误时重定向页面

时间:2014-09-18 08:52:43

标签: asp.net-mvc exception

我在MVC中遇到此错误。当我收到此错误时,我想重定向一个错误页面。怎么办?

错误是:

  

错误:

     

处理您的请求时出错。

2 个答案:

答案 0 :(得分:1)

你总是可以使用try catch。

以下是一个例子。希望它有所帮助

    // action method for same
    [HttpPost]  
    public ActionResult Save(EmployeeModel employeeModel)
    {

        try
        {
            // do something, like call a service/business layer method.

            // redirect to home action
             return RedirectToAction("Home");

        }
        catch (Exception ex)
        {
            // if failure gets into catch block and returns the posted model back, 
            // work aroud for viewstate(does not exist in asp.net mvc) using model binding in asp.net mvc
            return View(employeeModel);

        }
    }

答案 1 :(得分:0)

应用try catch块并在catch块中使用重定向代码重定向网页...我希望这会对你有所帮助