모든 404 오류를 처리하기 위해 사용자 지정 HttpHandler를 작성하려고합니다. 핸들러는 내가 지정한 모든 파일 유형을 포착하고 처리하지만 어떤 이유로 폴더에 대한 요청을 처리하지 않습니다. 즉, mysite.com/foo/bar.html 또는 mysite.com/foo/bar를 입력하면됩니다. aspx는 그것을 처리하고 올바른 오류 페이지를 보여 주지만 mysite.com/foo/에 들어가면 소스 코드가 없거나 전혀없는 빈 페이지가 표시됩니다. 의 Web.config의 관련 섹션을ASP.NET에서 폴더 요청을 처리하지 않음
public class RedirectHttpModule :IHttpHandler, IHttpModule {
public RedirectHttpModule() {
//
// TODO: Add constructor logic here
//
}
public void Dispose() { }
public void Init(HttpApplication context) {
context.Error += new EventHandler(ErrorHandler);
}
private void ErrorHandler(object sender, EventArgs e) {
HttpApplication application = (HttpApplication)sender;
application.Context.Response.TrySkipIisCustomErrors = true;
Exception lastError = application.Server.GetLastError();
HttpException ex = lastError as HttpException;
ILog _logger = LogManager.GetLogger(typeof(Page));
string page = "~/404.aspx";
if (ex != null) {
application.Server.ClearError();
application.Context.Handler = System.Web.UI.PageParser.GetCompiledPageInstance(page, application.Server.MapPath(page), application.Context);
string username = application.Context.User.Identity.Name;
if (!String.IsNullOrEmpty(username)) _logger.ErrorFormat("HTTP Error {0}: {1} Username: {2}", ex.GetHttpCode().ToString(), ex.Message, username);
else _logger.ErrorFormat("HTTP Error {0}: {1}", ex.GetHttpCode().ToString(), ex.Message);
}
else {
application.Context.Handler = System.Web.UI.PageParser.GetCompiledPageInstance(page, application.Server.MapPath(page), application.Context);
}
}
public bool IsReusable {
get { return true; }
}
public void ProcessRequest(HttpContext context) {
if (!File.Exists(context.Request.PhysicalPath)) {
throw new HttpException(404, String.Format("The file or directory {0} does not exist.", context.Request.PhysicalPath));
}
else {
context.Response.TransmitFile(context.Request.PhysicalPath);
}
}
}
과 여기에 : 여기 핸들러의 코드입니다
<handlers>
<add name="html-to-aspx-isapi" path="*.html" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" preCondition="classicMode,runtimeVersionv2.0,bitness32" />
<add name="html-to-aspx" path="*.html" verb="*" type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" preCondition="integratedMode" />
<add name="htm-to-aspx-isapi" path="*.htm" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" preCondition="classicMode,runtimeVersionv2.0,bitness32" />
<add name="htm-to-aspx" path="*.htm" verb="*" type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" preCondition="integratedMode" />
<add name="asp-to-aspx-isapi" path="*.asp" verb="*" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" preCondition="classicMode,runtimeVersionv2.0,bitness32" />
<add name="asp-to-aspx" path="*.asp" verb="*" type="System.Web.UI.PageHandlerFactory" resourceType="Unspecified" preCondition="integratedMode" />
<add name="RedirectHttpModule" modules="RedirectHttpModule" preCondition="" path="*" verb="*" resourceType="Either"/>
</handlers>
<modules runAllManagedModulesForAllRequests="true">
<add name="RedirectHttpModule" type="RedirectHttpModule" preCondition="managedHandler"/>
</modules>
어떤 이유로, 대신 클래식의 통합을 실행중인 경우에도, 처음 6 개 핸들러를 제거하면 ASP.NET에서 html, htm 또는 asp 요청을 더 이상 처리하지 않습니다. 일종의 구성 문제가 의심됩니다. 어떤 아이디어?
미리 도움 주셔서 감사합니다.
또한 왜 Web.config의 customErrors 섹션을 사용하지 않는지 궁금합니다. –
로깅과 customErrors가 어떤 이유로 든 인증되지 않으면 올바르게 작동하지 않는다는 사실 즉, 404 오류에 대한 표준 "서버 오류/애플리케이션"페이지를 제공합니다. 그 문제를 다루는 것이 더 간단했을 것입니다. 그러나 이것은 좋은 학습 경험입니다 :) –