【发布时间】:2017-05-07 19:49:23
【问题描述】:
我想要一本可以添加/删除条目的字典
我在这里看到了这个答案,但它对我不起作用
NancyFX: How to use bootstrapper to persist an object 但我无法保留字典中的条目
有没有办法让它工作?
【问题讨论】:
标签: c# rest dictionary nancy
我想要一本可以添加/删除条目的字典
我在这里看到了这个答案,但它对我不起作用
NancyFX: How to use bootstrapper to persist an object 但我无法保留字典中的条目
有没有办法让它工作?
【问题讨论】:
标签: c# rest dictionary nancy
我找到了方法 只需将我的字典设置为静态或静态类 通过这种方式,我将始终访问它并向其添加条目并保留我的条目
【讨论】:
您可以使用 Nancy 使用的 DI 框架来指定模块的依赖关系。 AsSingleton() 帮助您为所有模块创建一个实例。
public class Bootstrapper : DefaultNancyBootstrapper
{
protected override void ConfigureApplicationContainer(TinyIoCContainer container)
{
base.ConfigureApplicationContainer(container);
container.Register<IMyDictionary, Dictionary<string,string>>().AsSingleton();
}
}
并包含这个
public class HomeModule : NancyModule
{
public HomeModule(IMyDictionary dict)
{
Get["/"] => dict;
}
}
【讨论】: