2013-03-09 21 views
0

的Web.configAsp.net MVC4,defaultRedirect不工作?

<customErrors mode="On" defaultRedirect="~/Error/HttpError"> // is not working 
    <error redirect="~/Error/NotFound" statusCode="404" /> 
    <error redirect="~/Error/HttpError" statusCode="500" /> // also it is not working 
</customErrors> 

差錯控制

public class ErrorController : Controller 
{ 
    // 
    // GET: /Error/ 

    public ActionResult HttpError() 
    { 
     return Content("HttpError was called!"); 
     //return View("Error"); 
    } 

    public ActionResult NotFound(string aspxerrorpath) 
    { 
     return View(); 
    } 

    public ActionResult Index() 
    { 
     return RedirectToAction("Index", "Home"); 
    } 
} 

對於404錯誤,它調用NOTFOUND行動爲好,但對於其他錯誤,它永遠不會打HttpError方法。

public ActionResult ETest() 
{ 
    throw new Exception("yahoo"); 
} 

我運行上面的測試代碼,它直接進入Error.cshtml頁面。

我做錯了什麼?

回答

0

你有沒有在你的FilterConfigs.cs中?

public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
{ 
    filters.Add(new HandleErrorAttribute()); 
} 

HandleError屬性是處理默認錯誤所必需的。

+0

是的,我有。但HttpError從未被調用。 – 2013-03-11 14:47:54

+0

@Expertwannabe - 你正在做一個遠程連接,對嗎?在本地計算機上調用時沒有看到錯誤處理程序。 – 2013-03-11 16:59:54

+0

@謝謝你的關心,我在本地機器上試過了,但它不起作用。我可以使用Application_error而不是CustomErrors來解決這個問題。 – 2013-03-11 20:18:50

相關問題