我很难使这个ConfigurationSection实现工作。这里的目标是为MIME文件类型提供可配置的文件扩展名映射。我按照两个教程试图达到这个设计,从来没有工作过。我怀疑是因为电话ConfigurationManager.GetSection("extensionFiletypes") as FilenameExtensionTypeConfiguration
返回null
。我做错了什么,如何解决?
的App.config
<configuration>
<configSections>
<section name="extensionFiletypes" type="Models.Config.FilenameExtensionTypeConfiguration, Models.Config"/>
</configSections>
<extensionFiletypes>
<collection>
<add extension="acx" type="application/internet-property-stream" />
<add extension="ai" type="application/postscript" />
...
<add extension="z" type="application/x-compress" />
<add extension="zip" type="application/zip" />
</collection>
</extensionFiletypes>
</configuration>
类:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Models.Config
{
public sealed class FilenameExtensionTypeConfiguration : ConfigurationSection
{
public static FilenameExtensionTypeConfiguration GetConfiguration()
{
var configuration = ConfigurationManager.GetSection("extensionFiletypes") as FilenameExtensionTypeConfiguration;
return configuration ?? new FilenameExtensionTypeConfiguration();
}
...
}