2010-11-24 11 views
0

は、私はこのような行動を持っている:どうすればasp.net mvcエラーを処理し、コントローラから同じビューを使用できますか?

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult New(Product product) 
{ 
    try 
    { 
     if(ModelState.IsValid) 
     { 
      _productService.Create(product); 
      TempData["success"] = "Product created successfully!"; 
      return RedirectToAction("Edit", new { product.Id }); 
     } 
    } 
    catch (Exception e) 
    { 
     Logger.Exception(e); 
     TempData["error"] = "Oops, an error occurred! Please try again in a few moments."; 
    } 

    return View(product); 
} 

は、私は方法のうちのロジックを処理し、このエラーをしたいです。しかし、デフォルトの[HandleError]のやり方ではなく、エラーの場合に別のビューにユーザーをリダイレクトするのではなく、TempData ["error"]と同じビューを返します。同じページの上部。

このtry {} catch {}コードをすべて削除してこのロジックをこのアクションの外側に置くこともできます。

答えて

0

[HandleError]とOnExceptionを使用して、必要な処理を行うことができます。

protected override void OnException(ExceptionContext filterContext) 
{ 
     // Output a nice error page 
     if (filterContext.HttpContext.IsCustomErrorEnabled) 
     { 
       filterContext.ExceptionHandled = true; 
       this.View("Error").ExecuteResult(this.ControllerContext); 
     } 
} 

は、私はしばらく前に私のブログにこのことについて、もう少し情報を掲載:

http://blog.dantup.com/2009/04/aspnet-mvc-handleerror-attribute-custom.html

例えば、カスタムビューを表示します
関連する問題