Bootstrapper - kotC9/VueNancy GitHub Wiki

Right smack in the center of Nancy lives this mythical unicorn that we like to call the Bootstrapper. Rather than being one monolithic framework, Nancy is actually lots of little pieces of functionality that are composed together when the application starts up. Configuration of this composition, including swapping out any of the default “pieces of the puzzle”, is done via the bootstrapper.

In this article, we will look at the configuration of what data will be allowed access. We will configure headers: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers

  1. Create class Bootstrapper inherited from class DefaultNancyBootstrapper
    public class Bootstrapper : DefaultNancyBootstrapper
    {
        protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
        {
            AllowAccessToConsumingSite(pipelines);
        }

        private static void AllowAccessToConsumingSite(IPipelines pipelines)
        {
            pipelines.AfterRequest.AddItemToEndOfPipeline(x =>
            {
                x.Response.Headers.Add("Access-Control-Allow-Origin", "*");
                x.Response.Headers.Add("Access-Control-Allow-Methods", "POST,GET");
                x.Response.Headers.Add("Access-Control-Allow-Headers", "Content-type");
            });
        }
    }

There are overrided method ApplicationStartup how unsurprisingly, called on startup.

In method AllowAccessToConsumingSite in this method are defined HTTP headers:

x.Response.Headers.Add("Access-Control-Allow-Origin", "*"); indicates what the response can be shared with all origins

x.Response.Headers.Add("Access-Control-Allow-Methods", "POST,GET"); allows only GET and POST methods

x.Response.Headers.Add("Access-Control-Allow-Headers", "Content-type"); indicate "Content-type" HTTP header can be used during the actual request.

  1. Add class instance in Main():
using var host = new NancyHost(new Bootstrapper(), configuration, bindUrl);