저는 ASP.Net MVC 프로젝트에서 동일한 문제를 다루기 위해 처리기를 통해 .Net을 통해 모든 것을 파이핑했습니다. ErrorController
먼저
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*.*" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*.*" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*.*" verb="GET,HEAD,POST,DEBUG,PUT,DELETE" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
는 요청 오류를 처리하기 위해 만든 찾을 수없는 요청은 좋아했다.
[AllowAnonymous]
public class ErrorController : Controller {
// GET: Error
public ActionResult NotFound() {
Response.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
Response.TrySkipIisCustomErrors = true;
HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
HttpContext.Response.TrySkipIisCustomErrors = true;
return View();
}
public ActionResult Error() {
Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
Response.TrySkipIisCustomErrors = true;
return View();
}
}
당신은 내가 IIS 오류
그리고 기본 컨트롤러가 ErrorController.NotFound
작업이 생성에 매핑하는 모든 알 수없는 작업을 처리 할 수
을 피하려고
TrySkipIisCustomErrors
전화를 알 수 있습니다.
public abstract class FrontOfficeControllerBase : Controller {
protected override void HandleUnknownAction(string actionName) {
var data = ViewData;
//Custom code to resolve the view.
//ViewResult view = this.View<ErrorController>(c => c.NotFound());
//Manually create view with view Data
ViewResult view = new ViewResult();
view.ViewData = new ViewDataDictionary();
view.ViewData["controller"] = "Error";
view.ViewData["action"] = "NotFound";
if (data != null && data.Count > 0) {
data.ToList().ForEach(view.ViewData.Add);
}
Response.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
Response.TrySkipIisCustomErrors = true;
view.ExecuteResult(this.ControllerContext);
}
}
모두 Controllers
은이 기본 컨트롤러를 상속합니다.
캐치 모든 경로는 다른 모든 경로 뒤에 구성되었습니다.
routes.MapRoute(
name: "404-NotFound",
url: "NotFound",
defaults: new { controller = "Error", action = "NotFound" }
);
routes.MapRoute(
name: "500-Error",
url: "Error",
defaults: new { controller = "Error", action = "Error" }
);
routes.MapRoute(
name: "CatchAll",
url: "{*any}",
defaults: new { controller = "Error", action = "NotFound" });
이
는 경로가 내 컨트롤러의 일치되지 않은 경우 확인했다 그것을
ErrorController.NotFound
작업을 안전하게 경로 것.
의 의견, 나는 각각의
NotFound.shtml
과
Views/Shared
폴더에 페이지 된
Error.cshtml
생성하고 난 당신이 찾고 있던 생각입니다 루트 레이아웃에 대한 액세스 혜택.
결국 모든 요청이 처리기에서 관리되고 그에 따라 라우팅되기 때문에 더 이상 필요가 없으므로 customErrors
과 httpErrors
을 모두 web.config
에서 제거 할 수있었습니다.
이 구조의 원래 아이디어는 내 요구 사항에 맞는/해결 된 솔루션을 찾을 때까지 사용 가능한 옵션을 혼합하고 일치시킨이 기사에서 비롯된 것입니다.이 도움이
Exception handling in ASP.NET MVC (6 methods explained)
희망.
내 제안을보고 시나리오에 적용 할 수 있는지 확인하십시오. – Nkosi