访问ASP.NET中的资源文件内容

时间:2014-01-13 11:09:47

标签: c# html asp.net .net

我在Resource1.resx中有一个HTML文件。现在,我必须使用StreamReader的html文件内容。我该怎么办?

以下是html文件的内容:

<html>
 <body>
  <table>
   <tr>
    <td>$$name$$</td>
   </tr>
   <tr>
    <td>$$subject$$</td>
   </tr>
  </table>
 </body>
</html>

我在cs中使用以下代码:

var assembly = Assembly.GetExecutingAssembly();
string resourceName = (string)GetGlobalResourceObject("Resource1", "MyTemplate");

using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
    string readfile = reader.ReadToEnd();
    string mystring = "";
    mystring = readfile;
    mystring = mystring.Replace("$$Member$$", name);
    mystring = mystring.Replace("$$Subject$$", TxtSubject.Text);
    return mystring;
}

stream会返回null

1 个答案:

答案 0 :(得分:0)

嘿,感谢所有人帮助我,但我找到了解决方案。这是代码:

var assembly = Assembly.GetExecutingAssembly();
string resourceName = (string)HttpContext.GetGlobalResourceObject("Resource1", "MyTemplate");
string mystring = "";
mystring = resourceName;
mystring = mystring.Replace("$$Member$$", name);
mystring = mystring.Replace("$$Subject$$", TxtSubject.Text);
return mystring;