InvalidOperationExceptionでカスタム404を表示するために、次のベースコントローラーを作成しました(たとえば、ビューが見つかりません)。
public class HandlesViewNotFoundController : Controller
{
protected override void OnException(ExceptionContext filterContext)
{
//InvalidOperationException is thrown if the path to the view
// cannot be resolved by the viewengine
if (filterContext.Exception is InvalidOperationException)
{
if (!filterContext.ExceptionHandled)
{
filterContext.ExceptionHandled = true;
filterContext.Result = new ViewResult { ViewName = "_404" };
filterContext.HttpContext.Response.StatusCode = 404;
Response.Clear();
// Clear the error on server.
Server.ClearError();
// Avoid IIS7 getting in the middle
Response.TrySkipIisCustomErrors = true;
}
}
base.OnException(filterContext);
}
}
何らかの理由で、404をトリガーするページを開くと、デバッグ時にローカルで、サーバーでページを表示するときにローカルで完全に機能しますが、サーバーでリモートで確認したのと同じURLを表示すると、内部サーバーエラー(500)が返されます。
何か案は?