JavaScript 클라이언트가 주로 액세스하는 API를 작성하므로 오류가 있는지 여부에 관계없이 모든 동작이 Json 결과를 반환합니다. 그렇게하면 클라이언트는 500 오류에서 XHR에 있거나 200 응답으로 반환 데이터 페이로드에 있든간에 되돌아 오는 것을 우아하게 처리 할 수 있습니다.ASP.NET MVC 3 RC1이 내 500 응답에 무언가를하고 있습니다.
[HttpPost]
public ActionResult EditName(string Name, string Id)
{
try
{
// some code here
Response.StatusCode = 200;
return Json(new APIResult(true, Name), JsonRequestBehavior.AllowGet);
}
catch (Exception ex)
{
Response.StatusCode = 500;
Response.StatusDescription = ex.UnRoll();
return Json(new APIResult(false, ex.UnRoll()), JsonRequestBehavior.AllowGet);
}
}
APIResult 객체와 .UnRoll() 메소드 나는 일관된 결과를 용이하게하기 위해 만든 구조입니다 : (MVC 2에서 잘 작동) 내 코드는 보통 이런 작은 선물 간다. 어쨌든, 예외가 처리되면 MVC 3 RC1에서 문제가 발생합니다. 상태 코드가 500로 설정되어 있기 때문에, ASP.NET 처리기는 피들러 지금 뭔가를한다는 것을, 나는 응답이 볼 수 있습니다 : 나는 그것이 적절한 JSON 개체가 있습니다 반환받을 수
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>500 - Internal server error.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans- serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;}
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;}
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;}
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
<div class="content-container"><fieldset>
<h2>500 - Internal server error.</h2>
<h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3>
</fieldset></div>
</div>
</body>
</html>
유일한 방법 Response.StatusCode = 500;
을 Response.StatusCode = 200;
으로 바꿉니다.이 코드는 클라이언트에서 XMLHttpRequest 객체에 의한 진정한 오류로 처리되지 않기 때문에 제가하고 싶은 것이 아닙니다.
누구나 내가 무엇이 바뀌 었는지, 또는 내가 지금 다르게해야 할 필요가있는 것을 지적 할 수 있습니까?
감사합니다.
'Response.TrySkipIisCustomErrors = true; '를 추가해 보셨습니까? – Buildstarted