public class HandlerFactory : IHttpHandlerFactory
{
    public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string virtualPath, string path)
    {
        context.Response.Write(requestType + "<br>");
        context.Response.Write(virtualPath + "<br>");
        context.Response.Write(path + "<br>");
        IHttpHandler page = BuildManager.CreateInstanceFromVirtualPath("~/Class3.cs", typeof(Page)) as Page;
        if (page == null)
        {
            return null;
        }
         return page;
    }    public virtual void ReleaseHandler(IHttpHandler handler)
    {
    }
}
只有标记为internal的CreateInstanceFromVirtualPath方法,而这个为pulic的CreateInstanceFromVirtualPath方法不行。运行结果为无法将类型为“System.Web.Compilation.BuildResultCompiledAssembly”的对象强制转换为类型“System.Web.Util.ITypedWebObjectFactory”。请问大侠该怎么办? 

解决方案 »

  1.   

    你在哪里见过.net framework使用CreateInstanceFromVirtualPath去装载什么 .cs 文件?而且还是在 ~/ 目录下?!无语。不知道还能说什么。
      

  2.   

    context.Server.Transfer(BuildManager.CreateInstanceFromVirtualPath(context.Request.Path, typeof(Page)) as IHttpHandler, true);
    http://msdn.microsoft.com/zh-cn/library/cc668202(VS.90).aspx
      

  3.   

    不要胡乱玩什么抽象。Redirect或者Transfer到一个正常的Page上。傻瓜式的编程,如果你发现人家重复50遍,每一遍都是那么从容淡定而且节奏精准,你就应该理解人家绝对不是傻瓜(相反要反思自己了)。我从来不信那些自认为构思很精巧,但是总是把握不了节奏的开发者。
      

  4.   

    如果你想干涉输出的html,就是用那个在参数中有TextHtmlWriter的 Server.Execute 方法,去调用另一个 Page,正规的Page,而不是什么 .cs文件。
      

  5.   


    我主要是不想用Page类来处理aspx页,我想自己弄个类来处理,但为什么不行呢?2楼说的方法不是一个目的。我是看到refelctor 反编译.net框架后看到微软自己用的那段代码,不过BuildManager.CreateInstanceFromVirtualPath有两个,一个是intenal,一个是public.微软当然用的是internal Page page = BuildManager.CreateInstanceFromVirtualPath("xxxxxx", typeof(Page)) as Page;
      

  6.   

    [PermissionSet(SecurityAction.LinkDemand, Unrestricted=true), PermissionSet(SecurityAction.InheritanceDemand, Unrestricted=true)]
    public class PageHandlerFactory : IHttpHandlerFactory2, IHttpHandlerFactory
    {
        // Fields
        private bool _isInheritedInstance;    // Methods
        protected internal PageHandlerFactory()
        {
            this._isInheritedInstance = base.GetType() != typeof(PageHandlerFactory);
        }    public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string virtualPath, string path)
        {
            return this.GetHandlerHelper(context, requestType, VirtualPath.CreateNonRelative(virtualPath), path);
        }    private IHttpHandler GetHandlerHelper(HttpContext context, string requestType, VirtualPath virtualPath, string physicalPath)
        {
            Page page = BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page), context, true, true) as Page;
            if (page == null)
            {
                return null;
            }
            page.TemplateControlVirtualPath = virtualPath;
            return page;
        }    public virtual void ReleaseHandler(IHttpHandler handler)
        {
        }    IHttpHandler IHttpHandlerFactory2.GetHandler(HttpContext context, string requestType, VirtualPath virtualPath, string physicalPath)
        {
            if (this._isInheritedInstance)
            {
                return this.GetHandler(context, requestType, virtualPath.VirtualPathString, physicalPath);
            }
            return this.GetHandlerHelper(context, requestType, virtualPath, physicalPath);
        }