如果在ASP.NET MVC中使用自定义配置,并且在调用GetSection
方法时返回了null
,可能是由于以下几个原因:
ConfigurationManager.GetSection
方法来加载配置文件。var config = ConfigurationManager.GetSection("customSection") as CustomSection;
GetSection
方法中传递的参数与配置文件中定义的配置节名称一致。配置文件示例如下:
ConfigurationSection
类。例如:public class CustomSection : ConfigurationSection
{
// 自定义配置属性
}
通过检查以上几个方面,您应该能够解决ASP.NET MVC自定义配置中GetSection
方法返回null
的问题。