Search code examples
asp.netcustom-server-controls

Reading an embedded file from an ASP.NET Custom Server Control an rendering it


I currently have a file "abc.htm" in my Custom Server Control Project and it's Build Action is set to Embedded Resource.

Now in the RenderContents(HtmlTextWriter output) method, I need to read that file and render it on the website.

I am trying the following but it's to no avail:

protected override void RenderContents(HtmlTextWriter output)
{
    var providersURL = Page.ClientScript.GetWebResourceUrl(typeof (OpenIDSel), "OpenIDSelector.Providers.htm");
    var fi = new FileInfo(providersURL); // <- exception here

    //the remaining code is to possibly render the file
}

This is an example of how the providersURL is:

/WebResource.axd?d=kyU2OiYu6lwshLH4pRUCUmG-pzI4xDC1ii9u032IPWwUzMsFzFHzL3veInwslz8Y0&t=634056587753507131

FileInfo is throwing System.ArgumentException: Illegal characters in path.


Solution

  • You could do something like :

    protected override void RenderContents(HtmlTextWriter output)
    {
    
     var source = ReadEmbeddedResource("AssemblyName", "OpenIDSelector.Providers.htm");    
    
     //the remaining code is to possibly render the file
    
    }
    
    
    private string ReadEmbeddedResource(string assemblyName, string resouceName)
    {
        var assembly = Assembly.Load(assemblyName);
        using (var stream = assembly.GetManifestResourceStream(resouceName))
        using(var reader = new StreamReader(stream))
        {
            return reader.ReadToEnd();
        }
    }