+1-888-365-2779
Try Now
More in this section

Forums / Bugs & Issues / VirtualPathProvider

VirtualPathProvider

4 posts, 0 answered
  1. florin
    florin avatar
    2 posts
    Registered:
    16 Sep 2012
    15 Dec 2008
    Link to this post
    I registered a new virtual path provider to access user controls embedded in dll. When i try to add the user control i get the error:"Exception Details: System.NullReferenceException: Object reference not set to an instance of an object."
    I mention that the same user control i can used it in other web applications.

  2. Vlad
    Vlad avatar
    498 posts
    Registered:
    15 Jul 2016
    16 Dec 2008
    Link to this post
    Hello,

    Could you please give us more details about the scenario? What do you mean by "When i try to add the user control"? Also we need your implementation of the virtual path provider in order to test it locally.

    All the best,
    Vlad
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. florin
    florin avatar
    2 posts
    Registered:
    16 Sep 2012
    16 Dec 2008
    Link to this post
        I registered a new module where i put the control that should take advantage of  registered provider. 

    The user control is added on page by dragging  it from the right panel with controls.
    Here is the Virtual path provider.  Even i registered the the provider the method  public override VirtualFile GetFile(string virtualPath) is never executed.
    1  public class AssemblyResourceProvider : System.Web.Hosting.VirtualPathProvider 
    2    { 
    3        public AssemblyResourceProvider() { } 
    4        private bool IsAppResourcePath(string virtualPath) 
    5        { 
    6            String checkPath = 
    7               VirtualPathUtility.ToAppRelative(virtualPath); 
    8            return checkPath.StartsWith("~/App_Resource/", StringComparison.InvariantCultureIgnoreCase); 
    9        } 
    10        public override bool FileExists(string virtualPath) 
    11        { 
    12            return (IsAppResourcePath(virtualPath) || base.FileExists(virtualPath)); 
    13        } 
    14        public override VirtualFile GetFile(string virtualPath) 
    15        { 
    16            if (IsAppResourcePath(virtualPath)) 
    17                return new AssemblyResourceVirtualFile(virtualPath); 
    18            else 
    19                return base.GetFile(virtualPath); 
    20        } 
    21        public override System.Web.Caching.CacheDependency GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart) 
    22        { 
    23            if (IsAppResourcePath(virtualPath)) 
    24                return null
    25            else 
    26                return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart); 
    27        } 
    28    } 
    29 
    30    class AssemblyResourceVirtualFile : VirtualFile 
    31    { 
    32        string path; 
    33        public AssemblyResourceVirtualFile(string virtualPath) 
    34            : base(virtualPath) 
    35        { 
    36            path = VirtualPathUtility.ToAppRelative(virtualPath); 
    37        } 
    38        public override System.IO.Stream Open() 
    39        { 
    40            string[] parts = path.Split('/'); 
    41            string assemblyName = parts[2]; 
    42            string resourceName = parts[3]; 
    43 
    44            assemblyName = Path.Combine(HttpRuntime.BinDirectory, assemblyName); 
    45 
    46            System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(assemblyName); 
    47            if (assembly != null
    48            { 
    49                return assembly.GetManifestResourceStream(resourceName); 
    50            } 
    51            return null
    52        } 
    53    } 
    Here is the method where i registered the provider.
     protected override void CreateChildControls() 
            { 
                this.Controls.Clear(); 
                System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(new AssemblyResourceProvider()); 
                string Url = string.Format("~/{0}/{1}/{2}"this.ResourceFolder, this.AssemblyName, this.AscxName); 
                this._control = Page.LoadControl(Url); 
                this.Controls.Add(this._control); 
            } 

  4. Vlad
    Vlad avatar
    498 posts
    Registered:
    15 Jul 2016
    19 Dec 2008
    Link to this post
    Hello Florin,

    Try to register your VirtualPathProvider on application start in the global.asax:
        void Application_Start(object sender, EventArgs e)  
        { 
            // Code that runs on application startup 
            AssemblyResourceProvider vpProvider = new AssemblyResourceProvider(); 
            System.Web.Hosting.HostingEnvironment.RegisterVirtualPathProvider(vpProvider); 
     
        } 
     


    Regards,
    Vlad
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Register for webinar
4 posts, 0 answered