Donny V. Donny V. - 11 months ago 157
C# Question

How to add a request header in Nancyfx?

I tried adding this in the bootstrapper in the ApplicationStartup override.

pipelines.AfterRequest.AddItemToStartOfPipeline(ctx =>
ctx.Request.Headers["x-fcr-version"] = "1";

Its giving me errors.

Can anyone point me in the right direction?

Answer Source

Notice how you are trying to set the Request while trying to manipulate the Response ?

Try this..

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

    pipelines.AfterRequest.AddItemToEndOfPipeline(c =>
        c.Response.Headers["x-fcr-version"] = "1";

This is what my Response looks like..

enter image description here

Or .. you can use Connection Negotiation if you're going to set it at the module level...

Get["/"] = parameters => {
    return Negotiate
        .WithModel(new RatPack {FirstName = "Nancy "})
        .WithMediaRangeModel("text/html", new RatPack {FirstName = "Nancy fancy pants"})
        .WithHeader("X-Custom", "SomeValue");