从ASP.NET自定义服务器控件中读取嵌入文件并对其进行渲染

时间:2010-03-31 17:02:41

标签: asp.net custom-server-controls

我的自定义服务器控件项目中目前有一个文件“abc.htm”,Build Action设置为Embedded Resource

现在在RenderContents(HtmlTextWriter output)方法中,我需要阅读该文件并在网站上进行渲染。

我正在尝试以下但是无济于事:

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
}

这是providersURL的示例:

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

FileInfo投掷System.ArgumentException:路径中存在非法字符。

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

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();
    }
}