0

I have a web api in Nancy 1.4.3. I have defined some settings in web.config under applicationSettings section. I was wondering how can I read these settings in a Nancy module (or Bootstrapper)? Because the conventional ways of reading these settings as in MVC/WebAPI are not available in Nancy. Please consider that I am using Nancy 1.4.3 not Nancy 2x and .net 4.6.1 not .net core.

For simplicity, I am writing how the applicationSettings section looks like in web.config:

<applicationSettings>
    <Applicaton1.Properties.Settings>
       <setting name="DefaultUserID" serializeAs="String">
         <value>BatchReader</value>
       </setting>
      <setting name="DefaultPaymentFrequencyCode" serializeAs="String">
        <value>0</value>
      </setting>
     <setting name="DefaultPaymentTypeCode" serializeAs="String">
        <value>1</value>
     </setting>
</Application1.Properties.Settings>

Syed
  • 329
  • 2
  • 6
  • 18

3 Answers3

1

You should be able to read it exactly the same as any asp.net app. Make sure you add reference to:

System.Configuration

In Web.config add your key:

<appSettings>
<add key="key" value="hello key" />
</appSettings>

Include System.Configuration in your Bootstrapper:

namespace Test
{
    using System.Configuration;
    using Nancy;
    using Nancy.Authentication.Forms;
    using Nancy.Bootstrapper;
    using Nancy.TinyIoc;


    public class Bootstrapper : DefaultNancyBootstrapper
    {
        protected override void ApplicationStartup (TinyIoCContainer container,
                                                   IPipelines pipelines)
        {
            base.ApplicationStartup (container, pipelines);
            StaticConfiguration.DisableErrorTraces = false;
            StaticConfiguration.EnableRequestTracing = true;
        }

        protected override void ConfigureApplicationContainer (TinyIoCContainer
                                                              container)
        {
            base.ConfigureApplicationContainer (container);

            var key = ConfigurationManager
                                       .AppSettings.Get ("key")

        }


        protected override void ConfigureRequestContainer (TinyIoCContainer container,
                                                          NancyContext context)
        {
            base.ConfigureRequestContainer (container, context);

        }

        protected override void RequestStartup (TinyIoCContainer container,
                                               IPipelines pipelines,
                                               NancyContext context)
        {
            base.RequestStartup (container, pipelines, context);
        }
    }
}

Thats it! :)

Fritz Seitz
  • 63
  • 2
  • 7
  • Thank you for your help! Can you please write .net core variant too? Or would that be simililar? (I just want to know that ahead so that when I would work in .net core, I know it already, if it is different)? Ofcourse, I know there is a difference that .net core app will have appsettings.json instead of web.config. :) – Syed Mar 07 '17 at 12:12
  • Using nancy with .net core as far as I know requires the use of prerelease versions 2+, if its not production app that is fine. Using the configuration manager with core is different refer to here: https://zimmergren.net/using-appsettings-json-instead-of-web-config-in-net-core-projects/ I haven't tried it yet with Nancy. – Fritz Seitz Mar 10 '17 at 20:10
  • Excellent article. Just what I was looking for. I owe youa big thanks :) – Syed Mar 14 '17 at 12:38
0

What conventional ways would you use in WebApi that are missing in Nancy ?

Sifiso Shezi
  • 486
  • 2
  • 6
0

For a web app I think you should really be using System.Web.Configuration:

using System.Web.Configuration

For example:

  var someVar = WebConfigurationManager.AppSettings["SomeSetting"];

See here for more information.

Community
  • 1
  • 1
Norbert Norbertson
  • 2,102
  • 1
  • 16
  • 28