获取ScriptHandlerFactory处理程序

有没有办法调用返回IHttpHandler类型对象的System.Web.Script.Services.ScriptHandlerFactory类GetHandler方法。 我知道ScriptHandlerFactory是一个内部类,但是我可以从其中获得它的任何其他方式吗? 我说的原因是因为我想路由我的.asmx路径并且路由需要这种类型的代码:

public IHttpHandler GetHttpHandler(RequestContext requestContext) { return new WebServiceHandlerFactory().GetHandler(HttpContext.Current, "*", _VirtualPath, HttpContext.Current.Server.MapPath(_VirtualPath)); } } 

上面使用了“WebServiceHandlerFactory”,但我只想使用ScriptHandlerFactory,因为我也想使用jquery ajax调用。 我将通过jquery ajax调用调用asmx方法。 你能帮我这么做吗?

借用Spring.Net。 我使用它来访问SimpleHttpHandler因为我使用IoC来创建HttpHandler

编辑: 这是我用来弄清楚如何做的代码 。 显然,它需要完全信任 ,我不喜欢这个hack,但是在创建我自己的需要依赖内置function的处理程序工厂时,ASP.NET并没有让我很容易做IoC。

编辑2:修复了System.Web.Services.Protocols.WebServiceHandlerFactory的完全限定类型名称。 修复了程序集搜索,从搜索System.Web IHttpHandlerSystem.Web中的WebService 。 如果您有任何问题,请告诉我。

编辑3:对此抱歉。 我省略了他们使用的SecurityCritical类,如果你愿意,你可以用另一种方式,但是当我这样做时,我只是直接从他们那里复制了。 它用于提升和断言权限,这就是您需要完全信任的原因

  static YourCodeHere() { PrivilegedCommand cmd = new PrivilegedCommand(); SecurityCritical.ExecutePrivileged(new PermissionSet(PermissionState.Unrestricted), new SecurityCritical.PrivilegedCallback(cmd.Execute)); handlerFactory = cmd.Result; } private class PrivilegedCommand { public IHttpHandlerFactory Result = null; public void Execute() { Type handlerFactoryType = typeof(System.Web.Services.WebService).Assembly.GetType("System.Web.Services.Protocols.WebServiceHandlerFactory"); Result = (IHttpHandlerFactory)Activator.CreateInstance(handlerFactoryType, true); } } ///  /// Utility class to be used from within this assembly for executing security critical code /// NEVER EVER MAKE THIS PUBLIC! ///  /// Erich Eichinger internal class SecurityCritical { internal delegate void PrivilegedCallback(); [SecurityCritical, SecurityTreatAsSafe] [MethodImpl(MethodImplOptions.NoInlining)] internal static void ExecutePrivileged(IStackWalk permission, PrivilegedCallback callback) { permission.Assert(); try { callback(); } finally { CodeAccessPermission.RevertAssert(); } } }