私は、NerdDinner プロジェクトから多くの作業をコピーした MVC プロジェクトに取り組んでいます。NerdDinner では、ディナーが見つからない場合、またはユーザーがディナーの所有者でない場合、DinnerNotFound、InvalidOwner などのいくつかのビューを返します。しかし、私のプロジェクトでは、ビュー (CustomException) を作成し、そのようなすべての理由でそれを使用したいと考えています。そのため、例外を発生させて、basecontroller の OnException イベントでそれらをキャッチします。そこから、ELMAH にログを記録した後、カスタム ビューをレンダリングします。
しかし、そのビューをレンダリングする呼び出し (RedirectToAction("CustomException",ce );) は機能していないようで、アクション CustomException に移動しません。
誰かが私を助けてくれますか?ここにすべてのファイルをリストしました。また、global.asax.cs ファイルにエントリを作成するにはどうすればよいですか。コードを以下に示します。
よろしくパーミンダー
ListingExceptions.cs
名前空間 Listing.Exceptions { public static class ListingExceptions {
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 { public partial class BaseController : Controller {
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
{
public virtual ActionResult Details(long id, string title) {
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( "Exception", // ルート名 "{controller}/{action}/{ce}", // パラメーターを持つ URL new { controller = "Base", action = "CustomException", ce = "" } );