1
나는 내 자신의 ExceptionFilter를 만들려고합니다. ASP.NET MVC는 기본적으로 [HandleError] 특성과 함께 제공됩니다. 이것은 대단합니다 ->하지만 일부 HTML 오류보기를 반환합니다.사용자 지정 ASP.NET MVC에 대한 도움이 필요합니다. IExceptionFilter
이와 같이 저는 일부 json 오류 메시지를 반환하려고합니다. 그래서 나는 내 것을 만들고있어.
이제 내 URL을 테스트 할 때까지 모든 것이 잘 작동합니다. 계속 오류가 있습니다. 이 메시지입니다 ....
C:\Temp\curl-7.19.5>curl -i http://localhost:6969/search/foo?name=1234&key=test1xxx
HTTP/1.1 401 Unauthorized
Server: ASP.NET Development Server/9.0.0.0
Date: Mon, 14 Sep 2009 01:54:52 GMT
X-AspNet-Version: 2.0.50727
X-AspNetMvc-Version: 1.0
Cache-Control: private
Content-Type: application/json; charset=utf-8
Content-Length: 6
Connection: Close
"Hi StackOverflow"'key' is not recognized as an internal or external command,
operable program or batch file.
C:\Temp\curl-7.19.5>
Ok - 그건 의미가 없습니다.
public class HandleErrorAsJson : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
// Snip normal checks and stuff...
// Assume we've figured out the type of error this is.
// I'm going to hardcode it here, right now.
int statusCode = 401;
string message = "Hi StackOverflow";
// Now prepare our json output.
filterContext.Result = new JsonResult
{
Data = message
};
// Prepare the response code.
filterContext.ExceptionHandled = true;
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = statusCode;
}
}
은 그래서 내 코드입니다 .... 그것은 그렇다고 일하고 있지만 그렇지 않아 ... 난 후, 할 노력하고있어 설명하기 위해 몇 가지 코드를 할 수 있습니다.
이 '핵심'의 의미는 무엇입니까? 나는 무엇을 그리워 했는가?
도와주세요!