从嵌入资源加载模板

发布于 2024-09-28 08:54:33 字数 70 浏览 7 评论 0原文

如何将嵌入资源加载为 ITemplate? LoadTemplate() 方法仅采用字符串虚拟路径,显然这不适用于嵌入式资源。

How can I load an embedded resource as an ITemplate? The LoadTemplate() method only takes a string virtual path, and obviously this will not work for embedded resources.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

牵强ㄟ 2024-10-05 08:54:33

假设您的模板是嵌入的并且需要保持这种方式(我认为您可能需要重新考虑),这是我不久前编写的一个函数,在处理嵌入文件(主要是 .sql 文件)时我已经成功使用过多次)。它将嵌入的资源转换为字符串。然后,您可能需要将模板写入磁盘。

public static string GetEmbeddedResourceText(string resourceName, Assembly resourceAssembly)
{
   using (Stream stream = resourceAssembly.GetManifestResourceStream(resourceName))
   {
      int streamLength = (int)stream.Length;
      byte[] data = new byte[streamLength];
      stream.Read(data, 0, streamLength);

      // lets remove the UTF8 file header if there is one:
      if ((data[0] == 0xEF) && (data[1] == 0xBB) && (data[2] == 0xBF))
      {
         byte[] scrubbedData = new byte[data.Length - 3];
         Array.Copy(data, 3, scrubbedData, 0, scrubbedData.Length);
         data = scrubbedData;
      }

      return System.Text.Encoding.UTF8.GetString(data);
   }
}

用法示例:

var text = GetEmbeddedResourceText("Namespace.ResourceFileName.txt",
                                   Assembly.GetExecutingAssembly());

Assuming that your templates are embedded and need to stay that way (which I think you may want to reconsider), here is a function I wrote a while back that I've used successfully many times when dealing with embedded files (mostly .sql files). It converts an embedded resource to a string. You may then need to write your template out to disk.

public static string GetEmbeddedResourceText(string resourceName, Assembly resourceAssembly)
{
   using (Stream stream = resourceAssembly.GetManifestResourceStream(resourceName))
   {
      int streamLength = (int)stream.Length;
      byte[] data = new byte[streamLength];
      stream.Read(data, 0, streamLength);

      // lets remove the UTF8 file header if there is one:
      if ((data[0] == 0xEF) && (data[1] == 0xBB) && (data[2] == 0xBF))
      {
         byte[] scrubbedData = new byte[data.Length - 3];
         Array.Copy(data, 3, scrubbedData, 0, scrubbedData.Length);
         data = scrubbedData;
      }

      return System.Text.Encoding.UTF8.GetString(data);
   }
}

Example usage:

var text = GetEmbeddedResourceText("Namespace.ResourceFileName.txt",
                                   Assembly.GetExecutingAssembly());
时间海 2024-10-05 08:54:33

您的控件应该如下所示:

public class Con : Control
{
    public Template Content { get; set; }

    protected override void CreateChildControls()
    {
        base.CreateChildControls();

        Content = new Template();

        // load controls from file and add to this control
        Content.InstantiateIn(this);
    }

    public class Template : ITemplate
    {
        public void InstantiateIn(Control container)
        {
            // load controls
            container.Controls.Add((HttpContext.Current.Handler as Page).LoadControl("Emb.ascx"));
        }
    }
}

然后嵌入文件:

<%@ Control Language="C#" %>

<asp:TextBox ID="Tb" runat="server" />

然后在使用该控件时它将加载嵌入资源,因此使用:

<%@ Register Assembly="TestWeb" Namespace="TestWeb" TagPrefix="c" %>
<c:Con runat="server" />

将创建一个 TextBox。


如果您尝试访问 DLL 内的文件,请查看 VirtualPathProvider 的此实现

Your control should look like it:

public class Con : Control
{
    public Template Content { get; set; }

    protected override void CreateChildControls()
    {
        base.CreateChildControls();

        Content = new Template();

        // load controls from file and add to this control
        Content.InstantiateIn(this);
    }

    public class Template : ITemplate
    {
        public void InstantiateIn(Control container)
        {
            // load controls
            container.Controls.Add((HttpContext.Current.Handler as Page).LoadControl("Emb.ascx"));
        }
    }
}

Then the embedded file:

<%@ Control Language="C#" %>

<asp:TextBox ID="Tb" runat="server" />

Then when using the control it will load the embedded resource, so using:

<%@ Register Assembly="TestWeb" Namespace="TestWeb" TagPrefix="c" %>
<c:Con runat="server" />

Will create a TextBox.


If you are trying to access a file inside a DLL, see this implementation of VirtualPathProvider.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文