asp.net - 生成临时文件的 ASPX 解析器/预编译器

标签 asp.net

有没有办法在预编译为临时文件之前更改 ASPX 文件源。

即删除空格和新行。

最佳答案

是的,有,但这并不容易,而且可能不值得您费心。

一种方法是创建自己的 BuildProvider 并在配置文件中用它替换默认的 System.Web.Compilation.PageBuildProvider :

<compilation debug="true">
    <buildProviders>
        <add extension=".aspx" type="MyProject.MyPageBuildProvider" />
    </buildProviders>
</compilation>

您还可以创建自己的 PageParser,很可能是从 TemplateParser 继承的。 BuildProvider 负责提供 PageParser。在大多数原始情况下,您可以覆盖 ParseFile 方法、读取 ASPX 文件、处理它、创建副本并将其传递给基本方法。

不幸的是,所有 ASPX 解析代码都是密封的,并且是 MS 库的内部代码,因此您无法继承。重写它意味着构建整个编译引擎。

另一种方法是创建您自己的页面构建器并将其放入属性中。缺点是您只能轻松访问第一级(页面)的文字(所有空格等)。要获得内部控件及其文字,您必须使用反射来破解解析器或(正确)操作代码 dom。通过这种方式,您将获得正确构建的 .cs 文件和临时程序集。

这是一个简化的示例:
namespace MyProject
{
    [FileLevelControlBuilder(typeof(MyPageBuilder))]
    public partial class _Default : System.Web.UI.Page
    {
        //this is Default.aspx
    }

    //The builder of the page
    public class MyPageBuilder : FileLevelPageControlBuilder
    {
        //This is where you'd strip white space, but only of top level,
        //such as between the head and form, or form and the end of file
        public override void AppendLiteralString(string text)
        {
            //let's replace some white spaces with garbage
            base.AppendLiteralString(text.Replace(" ", "#").Replace("\t", "@").Replace("\r", "$").Replace("\n", "%"));
        }

        //Here you can manipulate the entire generated code using CodeDom
        public override void ProcessGeneratedCode(System.CodeDom.CodeCompileUnit codeCompileUnit, System.CodeDom.CodeTypeDeclaration baseType, System.CodeDom.CodeTypeDeclaration derivedType, System.CodeDom.CodeMemberMethod buildMethod, System.CodeDom.CodeMemberMethod dataBindingMethod)
        {
            base.ProcessGeneratedCode(codeCompileUnit, baseType, derivedType, buildMethod, dataBindingMethod);
        }

        //Alternatively, you can "hack" the PageParser here using reflection
        //However, the _text field at this point is irrelevant, so it can't be used
        public override void Init(TemplateParser parser, ControlBuilder parentBuilder, Type type, string tagName, string ID, System.Collections.IDictionary attribs)
        {
            FieldInfo fi = parser.GetType().BaseType.BaseType.BaseType.GetField("_text", System.Reflection.BindingFlags.FlattenHierarchy | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
            string s = (string) fi.GetValue(parser);
            fi.SetValue(parser, s.Replace("\t", "*"));

            base.Init(parser, parentBuilder, type, tagName, ID, attribs);
        }
    }
}

在我看来,这是不值得的。

已编辑 错别字

关于asp.net - 生成临时文件的 ASPX 解析器/预编译器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/661885/

相关文章:

c# - 在 gridview 中使用 HyperLinkField 进行 URL 导航

javascript - jQuery 对话框不响应键盘按下

c# - 模型绑定(bind) CSP 报告 json

html - 为什么我在 asp.net 中丢失了 CSS?

c# - javascript 请求.QueryString

c# - 如何在运行时传递 ListView 的行索引?

c# - ASP.NET jQuery 的 ajax 函数问题

c# - HttpWebRequest 与 webclient 类谁更好

c# - 管理数据库中持久的动态网站设置

html - 无法在 View 中居中输入字段 (ASP.NET MVC)