1
내 mvc 3 (면도기) 응용 프로그램에 대한 사용자 지정 예외를 만들려고합니다. 하지만 제대로 작동하지 않습니다.오류 MVC 3 예외 필터를 추가하는 동안
다음은 맞춤 예외 클래스 용으로 작성한 코드입니다. 다음은
using System;
using System.Web.Mvc;
namespace TestApp.Helpers
{
public class CustomExceptionAttribute : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
if (!filterContext.ExceptionHandled && filterContext.Exception is Exception)
{
//filterContext.Result = new RedirectResult("/shared/Error.html");
filterContext.Result = new ViewResult { ViewName = "Error" };
filterContext.ExceptionHandled = true;
}
}
}
}
컨트롤러에 코드입니다 : 아래
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using TestApp.Domain;
using TestApp.Helpers;
namespace TestApp.Controllers
{
[CustomException]
public class MyController : Controller
{
private TestAppEntities db = new TestAppEntities();
public ActionResult Create(int id)
{
// Throwing exception intentionally
int a = 1;
int b = 0;
int c = a/b;
//This is another method which is working fine.
return View(CreateData(id, null));
}
}
}
과의 코드는 'Error.cshtml'
우리는 응용 프로그램을 실행할 때의 오류가 발생@model System.Web.Mvc.HandleErrorInfo
@{
ViewBag.Title = "Error";
}
<h2>
Sorry, an error occurred while processing your request.
</h2>
<div>
<p>
There was a <b>@Model.Exception.GetType().Name</b> while rendering <b>@Model.ControllerName</b>'s
<b>@Model.ActionName</b> action.
</p>
<p>
The exception message is: <b><@Model.Exception.Message></b>
</p>
<p>Stack trace:</p>
<pre>@Model.Exception.StackTrace</pre>
</div>
@ Model.Exception.GetType(). 모델이 null이기 때문에 이름. 이것은 정확한 오류입니다. NullReferenceException : 개체 참조가 개체의 인스턴스로 설정되지 않았습니다.
오류의 정확한 원인이 무엇인지 알려주실 수 있습니까? 어떻게 해결할 수 있습니까?
왜 사용자 정의 예외 필터가 필요합니까? 사용자 정의 구현은 내장 (HandleErrorFilter)과 완전히 동일합니다. 핸들 오류 정보 인스턴스를 전달하지 않는 예외가 발생합니다. – VJAI
마크, 'HandleErrorInfo'인스턴스가 누락 된 정확한 지점을 말씀해 주시겠습니까? 또한 다음과 같은 방법으로 기본 제공 특성을 사용해 보았습니다. [HandleError (ExceptionType = typeof (Exception), View = "오류")] 이 메시지는 오류 페이지로 리디렉션되지 않고 '노란 죽음의 화면' –
내 대답 참조. 왜 ExceptionType을 지정하고 있습니까? 그것은 꼭 필요한 것이 아닌가? – VJAI