Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a request header in Nancyfx?

Tags:

c#

web

nancy

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?

like image 231
Donny V. Avatar asked Jan 09 '23 23:01

Donny V.


1 Answers

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"})
        .WithView("negotiatedview")
        .WithHeader("X-Custom", "SomeValue");
};
like image 137
Pure.Krome Avatar answered Jan 11 '23 12:01

Pure.Krome