asp.net - 以编程方式注册自定义 ASP.NET ExpressionBuilder

标签 asp.net webforms web-config expressionbuilder

是否可以在应用程序启动期间以编程方式注册自定义 ExpressionBuilder 类?

等效的 web.config 声明显然是:

  <system.web>
    <compilation>
      <expressionBuilders>
        <add expressionPrefix="MyPrefix" type="MyNamespace.MyExpressionBuilder">
      </expressionBuilders>
    </compilation>
  </system.web>

如何在自定义 HTTP 模块 Init()、应用程序启动或 BeginRequest 期间动态注册它,而不修改 web.config?

最佳答案

我找到了ExpressionBuilderCollection Class

其中包括以下示例

class UsingExpressionBuildCollection {
    static void Main(string[] args)
    {
      try
      {
        // Set the path of the config file.
        string configPath = "";

        // Get the Web application configuration object.
        Configuration config =
          WebConfigurationManager.OpenWebConfiguration(configPath);

        // Get the section related object.
        CompilationSection configSection =
          (CompilationSection)config.GetSection("system.web/compilation");

        // Display title and info.
        Console.WriteLine("ASP.NET Configuration Info");
        Console.WriteLine();

        // Display Config details.
        Console.WriteLine("File Path: {0}",
          config.FilePath);
        Console.WriteLine("Section Path: {0}",
          configSection.SectionInformation.Name);

        // Create a new ExpressionBuilder reference.
        ExpressionBuilder myExpressionBuilder =
          new ExpressionBuilder("myCustomExpression", "MyCustomExpressionBuilder");
        // Add an ExpressionBuilder to the configuration.
        configSection.ExpressionBuilders.Add(myExpressionBuilder);

        // Add an ExpressionBuilder to the configuration.
        ExpressionBuilder myExpressionBuilder2 =
          new ExpressionBuilder("myCustomExpression2", "MyCustomExpressionBuilder2");
        configSection.ExpressionBuilders.Add(myExpressionBuilder2);

        // Display the ExpressionBuilder count.
        Console.WriteLine("ExpressionBuilder Count: {0}",
          configSection.ExpressionBuilders.Count);

        // Display the ExpressionBuildersCollection details.
        int i = 1;
        int j = 1;
        foreach (ExpressionBuilder expressionBuilder in configSection.ExpressionBuilders)
        {
          Console.WriteLine();
          Console.WriteLine("ExpressionBuilder {0} Details:", i);
          Console.WriteLine("Type: {0}", expressionBuilder.ElementInformation.Type);
          Console.WriteLine("Source: {0}", expressionBuilder.ElementInformation.Source);
          Console.WriteLine("LineNumber: {0}", expressionBuilder.ElementInformation.LineNumber);
          Console.WriteLine("Properties Count: {0}", expressionBuilder.ElementInformation.Properties.Count);
          j = 1;
          foreach (PropertyInformation propertyItem in expressionBuilder.ElementInformation.Properties)
          {
            Console.WriteLine("Property {0} Name: {1}", j, propertyItem.Name);
            Console.WriteLine("Property {0} Value: {1}", j, propertyItem.Value);
            ++j;
          }
          ++i;
        }

        // Remove an ExpressionBuilder.
        configSection.ExpressionBuilders.RemoveAt
          (configSection.ExpressionBuilders.Count-1);

        // Remove an ExpressionBuilder.
        configSection.ExpressionBuilders.Remove("myCustomExpression");

        // Update if not locked.
        if (!configSection.SectionInformation.IsLocked)
        {
          config.Save();
          Console.WriteLine("** Configuration updated.");
        }
        else
        {
          Console.WriteLine("** Could not update, section is locked.");
        }
      }

      catch (Exception e)
      {
        // Unknown error.
        Console.WriteLine(e.ToString());
      }

      // Display and wait.
      Console.ReadLine();
    }
  }
}

重要的部分是

Configuration config =
      WebConfigurationManager.OpenWebConfiguration(configPath);

// Get the section related object.
CompilationSection configSection =
  (CompilationSection)config.GetSection("system.web/compilation");

//...

// Create a new ExpressionBuilder reference.
var myExpressionBuilder = new ExpressionBuilder(
    expressionPrefix: "MyPrefix", 
    theType: "MyNamespace.MyExpressionBuilder"
);
// Add an ExpressionBuilder to the configuration.
configSection.ExpressionBuilders.Add(myExpressionBuilder);

//...

// Update if not locked.
if (!configSection.SectionInformation.IsLocked) {
  config.Save();
}

以上内容与此配置示例等效。

  <system.web>
    <compilation>
      <expressionBuilders>
        <add expressionPrefix="MyPrefix" type="MyNamespace.MyExpressionBuilder">
      </expressionBuilders>
    </compilation>
  </system.web>

这样可以首先搜索您的自定义构建器,并在必要时添加它,检查版本...等。

更新解决有关修改配置文件的问题

查看 .net 引用源后,页面解析器似乎通过内部 ExpressionBuilder.GetExpressionBuilder 直接从配置加载表达式构建器。 .

除了迄今为止所展示的能够以编程方式注入(inject)自定义构建器的扩展点之外,我还没有找到任何其他扩展点。

而且考虑到 System.Web 的闭源性质,我怀疑将来是否会出现这样的情况。

关于asp.net - 以编程方式注册自定义 ASP.NET ExpressionBuilder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52669480/

相关文章:

javascript - 通过 jQuery AJAX 上传文件后 HttpPostedFile.ContentLength 始终为 -2

c# - 动态切换到无cookie session 状态asp.net?

c# - 无法将类型为 'System.TimeSpan' 的对象转换为类型 'System.IConvertible'

asp.net - 如何在列表项中允许 html 控制?

asp.net - 我的事件日志充满了 ViewState 无效和未处理的异常

c# - 如何永久存储cookie

c# - 如何在没有 Shift、Control 或 Alt 要求的情况下将键盘快捷键分配给 asp.net 按钮?

jquery - ASP.NET 隐藏和显示 div

c# - 允许任何用户在没有凭据的情况下访问 'signalr/hubs',使用 cors(服务器运行 windows 身份验证)

asp.net - 如何在 asp.net 中启用 gzip?