gpt4 book ai didi

.net - 使用 aspx 渲染引擎生成非 HTML

转载 作者:行者123 更新时间:2023-12-04 04:14:26 26 4
gpt4 key购买 nike

是否可以使用 asp 模板引擎(使用部分代码隐藏类、动态 <% ... %> block 等)来生成非 HTML?我希望有一种干净且可维护的方式来动态生成代码。 (具体来说,我想生成填充数据库中的值的 LaTeX 代码。)

目前,我的 LaTeX 模板是带有占位符的资源字符串,我将这些字符串替换为数据库值。这种解决方案很快变得非常难以维护和清洁。我真的很想使用 aspx 标记中的动态 block ,但我不确定 a) 当输出不是 HTML 时,aspx 是否会合适,或者 b) 如何将结果实际呈现到 .tex 文件中.

生成代码本身位于 C# .dll 中。我们正在使用 .NET 3.5

这可能吗?提前致谢。

最佳答案

Visual Studio 2008 native 或 Visual Studio 2005 SDK 附带的 T4 模板,您几乎可以生成任何您想要的东西。

您可以通过以下链接获得更多信息:

  • Scott Hanselman's Blog
  • Rob Conery's Blog
  • Oleg Sych's Blog这是 T4 示例的完整存储库
  • Johan Danforth's Blog

  • 我很确定所有这些链接都是您探索的良好开端。

    如果要在 Visual Studio 之外生成 T4 模板,可以使用自定义 MSBuild 任务来调用 T4 模板 ( link)

    下面是 MSBuild 任务的“执行”代码示例。 Click here for the source code :
    public override bool Execute()
    {
    bool success = false;

    //read in the template:
    string template = File.ReadAllText(this.TemplatePath);

    //replace tags with property and item group values:
    ProjectHelper helper = new ProjectHelper(this);
    template = helper.ResolveProjectItems(template);

    //copy the template to a temp file:
    this._tempFilePath = Path.GetTempFileName();
    File.WriteAllText(this._tempFilePath, template);

    //shell out to the exe:
    ProcessHelper.Run(this, TextTransform.ToolPath, TextTransform.ExeName, string.Format(TextTransform.ArgumentFormat, this.OutputPath, this._tempFilePath));
    success = true;

    return success;
    }

    关于.net - 使用 aspx 渲染引擎生成非 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/227132/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com