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

以编程方式注册自定义ASP.NET ExpressionBuilder,asp.net,webforms,web-config,expressionbuilder,Asp.net,Webforms,Web Config,Expressionbuilder,是否可以在应用程序启动期间以编程方式注册自定义的ExpressionBuilder类 等价的web.config声明显然是: <system.web> <compilation> <expressionBuilders> <add expressionPrefix="MyPrefix" type="MyNamespace.MyExpressionBuilder"> </expressionB

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

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

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

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

我找到了

其中包括以下示例

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>

我还没有找到任何其他的可扩展性点,除了到目前为止所展示的能够注入定制构建程序programmaticaly的扩展点之外


同样考虑到System.Web的封闭源代码特性,我怀疑将来任何时候都会有。

正确,但是,我担心
config.Save()
实际上会试图写回物理Web.config文件并进行更新,因为这不是我想要的。是这样做的吗?@qJake yes save可用于更新或创建新的配置文件。@qJake在查看.net引用源之后,页面解析器似乎直接从配置中通过内部命令加载表达式生成器。我找不到任何其他的扩展点,除了到目前为止已经显示的可以注入构建器的扩展点。谢谢你的信息。我们必须评估是否要以编程方式或通过其他方式实际更新web.config,但最好了解API的工作原理。谢谢