我一直在尝试对 Elmah 做类似的事情。
我的路线设置如下:
var url = "admin/elmah.axd/{*pathInfo}";
var defaults = new RouteValueDictionary {{"pathInfo", string.Empty}};
var routeHandler = new ElmahHandler();
var route = new Route(url, defaults, routeHandler);
RouteTable.Routes.Add(route);
但我也发现路径信息总是空的,所以样式表和附加路径不起作用。我设法使用反射来调用 ErrorLogFactory 中的底层方法来实现我的目标。
private object InvokeMethod(string name, params object[] args)
{
var dynMethod = typeof(ErrorLogPageFactory).GetMethod(name, BindingFlags.Static | BindingFlags.NonPublic);
return dynMethod.Invoke(null, args );
}
然后我的处理程序看起来像这样
public class ElmahHandler : ErrorLogPageFactory, IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
var handler = InvokeMethod("FindHandler", requestContext.RouteData.Values["pathInfo"]) as IHttpHandler;
if (handler == null)
throw new HttpException(404, "Resource not found.");
var num = (int)InvokeMethod("IsAuthorized", context);
if (num != 0 && (num >= 0 || HttpRequestSecurity.IsLocal(context.Request) /*|| SecurityConfiguration.Default.AllowRemoteAccess*/))
{
return handler;
}
//new ManifestResourceHandler("RemoteAccessError.htm", "text/html").ProcessRequest(context);
HttpResponse response = context.Response;
response.Status = "403 Forbidden";
response.End();
return null;
}
}
我不需要让 ManifestResourceHandler 工作,所以只需将其注释掉,同样适用于 allowRemoteAccess 设置