현재 .Net Core 2.2 예외 처리 시나리오에서 작업 중입니다. UseDeveloperExceptionPage()
개발 모드에서 예외 정보를 표시하기 위해 를 사용하고 있습니다. 그러나 컨트롤러 메서드 중 하나에 Ajax를 수행 할 때 응용 프로그램이 런타임 예외를 throw하는 경우. UseDeveloperExceptionPage
대신 Ajax error
function () 에서 응답을 받고 있으므로 예외 세부 정보가 표시되지 않습니다 . 누구든지 개발자 예외 페이지가 표시되지 않는 이유를 설명 할 수 있습니까? 그리고 ajax 호출 중에 예외 세부 사항과 함께 개발자 예외 페이지를 표시하는 방법을 설명해주십시오.
아래는 Configure()
내 Startup.cs
파일에 있습니다.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseStatusCodePagesWithReExecute("/Error/{0}");
}
else
{
app.UseExceptionHandler("/Exception");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
//app.UseAuthentication();
app.UseSession();
//app.UseCookiePolicy();
app.UseMiddleware<AuthenticationMiddleware>();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Account}/{action=Index}/{id?}");
});
}
자바 스크립트
$.ajax({
url: "@Url.Action("Index", "Home")",
dataType: "json",
type: "POST",
Async: true,
success: function(result) {
// Do Something
},
error: function() {
alert("Oops!! Error Occured");
}
});
HomeController
public IActionResult Index()
{
throw new Exception();
return View(viewTransactionsBatchSearch);
}
다음과 같은 예외 정보를 얻을 수 있습니다.
$.ajax({
url: "@Url.Action("Index", "Home")",
dataType: "json",
type: "POST",
Async: true,
success: function (result) {
// Do Something
},
error: function (result) {
//do something with the responseText which contains the Body of the response
alert(result.responseText);
}
});
컨트롤러는 AJAX를 사용하여 호출했는지 여부를 알지 못합니다. Http 요청 만 확인합니다. 오류 함수에 매개 변수를 추가하여 Http 응답을 검사 할 수 있습니다.
예를 들어 responseText 속성을 검사 하여 예외 정보를 가져올 수 있습니다.
예를 들어 콘텐츠를 div 또는 기타 표시 요소로 설정할 수 있습니다.
또한 AJAX 요청의 dataType이 "json"으로 설정되어 있기 때문에 reponseText에는 예외 정보와 헤더 만 포함됩니다.
dataType을 "html"로 설정하면 FULL Developer Exception Page의 html이 표시되며이를 사용하여 작업을 수행 할 수 있습니다.
다음은 responseText로 수행 할 작업에 대한 몇 가지 예입니다.
예 1 : dataType : "html":
$.ajax({
url: "@Url.Action("Index", "Home")",
dataType: "html",
type: "POST",
Async: true,
success: function (result) {
// Do Something
},
error: function (result) {
document.open();
document.write(result.responseText);//this will replace the current page with the developer exception page
document.close();
}
});
예 2 when dataType : "json": 서버가 전체 HTML 문서를 반환하지 않도록 JSON을 요청했기 때문에 이것은 다릅니다.
이것은 "예쁘게"보이지 않지만 모든 예외 정보를 가지고 있습니다.
$.ajax({
url: "@Url.Action("Index", "Home")",
dataType: "json",
type: "POST",
Async: true,
success: function (result) {
// Do Something
},
error: function (result) {
document.body.innerHTML = result.responseText;
}
});
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다