2009-07-26 10 views
2

私はNerdDinnerプロジェクトから多くの作業をコピーしたMVCプロジェクトに取り組んでいます。 NerdDinnerでは、ディナーが見つからない場合、またはユーザーがディナーのオーナーでない場合、DinnerNotFound、InvalidOwnerなどのいくつかのビューを返します。しかし、私のプロジェクトでは、 ビュー(CustomException)を作成し、そのような理由で使用したいと考えています。だから私は例外を発生させ、私のbasecontrllerのOnExceptionイベントでそれらをキャッチします。そこから、私はELMAHにログオンした後にカスタムビューをレンダリングしたい。MVC質問Elmah

このビューをレンダリングする呼び出し(RedirectToAction( "CustomException"、ce);) は動作していないようですが、CustomExceptionアクションにナビゲートします。

誰かが理由を教えてくれますか?私はここにすべてのファイルをリストアップしました。 また、global.asax.csファイルにどのように入力する必要がありますか。 コードを以下に示します。

よろしくParminder

ListingExceptions.cs

名前空間Listing.Exceptions {パブリック静的クラスListingExeceptions {

public static CustomException GetCustomException(Exception ex) 
    { 
     CustomException ce = new CustomException(); 
     switch (ex.Message) 
     { 
      case ItemConstant.INVALID_OWNER: 
       ce = new CustomException("Invalid Owner", "OOps you are not owner of this item"); 
       break; 
      case ItemConstant.ITEM_NOT_FOUND: 
       ce = new CustomException("Item not Found", "The Item you are looking for couldnt be found"); 
       break; 
      default: 
       ce = new CustomException("Error ", "There is an Error."); 
       break; 
     } 

     return ce; 

    } 


} 

}

BaseController.cs

名前空間Listing.Controllers {パブリック部分クラスBaseController:コントローラ {

public virtual ActionResult CustomException(CustomException ce) 
    { 
     return View(ce); 
    } 

    protected override void OnException(ExceptionContext filterContext) 
    { 
     base.OnException(filterContext); 

     CustomException ce = ListingExeceptions.GetCustomException(filterContext.Exception); 
     ErrorSignal.FromCurrentContext().Raise(filterContext.Exception); 
     filterContext.ExceptionHandled = true; 

     RedirectToAction("CustomException",ce); 
    } 
} 

}

ListingController.cs

名前空間Listing.Controllers

{

パブリック仮想のActionResult詳細(長いID、文字列のタイトル) {

 Item item = itemRepository.GetItemByID(id); 

     if (item == null) 

     { 

      throw new Exception("ItemNotFound"); 

     } 

     else 

     { 

      return View("Details", new ListingFormViewModel(item, photoRepository)); 

     } 
    } 

}

global.asax.cs

routes.MapRoute( "例外"、//ルート名 " {controller}/{action}/{ce} "、//パラメータ付きのURL new {controller =" Base "、action =" CustomException "、ce =" "});

+0

は、あなたの質問/タイトルを言い替えるしてください。 – Soviut

答えて

1

OnExceptionのようにリダイレクトすることはできません。 filterContext.Result =を追加すると、動作するはずです:それは実際にあなたが解決しようとしているかを説明するよう

protected override void OnException(ExceptionContext filterContext) 
{ 
    base.OnException(filterContext); 

    CustomException ce = ListingExeceptions.GetCustomException(filterContext.Exception); 
    ErrorSignal.FromCurrentContext().Raise(filterContext.Exception); 
    filterContext.ExceptionHandled = true; 

    filterContext.Result = RedirectToAction("CustomException",ce); 
} 
+0

助けました。感謝のeyenz – Parminder