CarterCommunity / Carter

Carter is framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing code to be more explicit and most importantly more enjoyable.
MIT License
2.06k stars 174 forks source link

Add custom header in an After Hook #209

Closed Matias-Barrios closed 4 years ago

Matias-Barrios commented 4 years ago

Hi Guys,

I am trying to add a custom header to each response my app is returning. I added this code in the startup.cs file but I'm getting an error stating the response already started :

 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
       {
           app.UseExceptionHandler("/errorhandler");
           app.UseCarter(new CarterOptions(after: ctx => HeaderInjector(ctx)));
       }
       public Task HeaderInjector(HttpContext context)
       {
           context.Response.Headers.Add("x-gamecentral-received", "game1");
           return Task.CompletedTask;
       }

Am I doing something wrong?

jchannon commented 4 years ago

Afraid not, if you have returned a response we don't wrap it then issue a response, ASP.NET Core spots response has started and won't allow modification, best bet is to set a response header in the Before hook

On Thu, 24 Oct 2019 at 19:51, Matias.Barrios notifications@github.com wrote:

Hi Guys,

I am trying to add a custom header to each response my app is returning. I added this code in the startup.cs file but I'm getting an error stating the response already started :



public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseExceptionHandler("/errorhandler");
app.UseCarter(new CarterOptions(after: ctx => HeaderInjector(ctx)));
}
public Task HeaderInjector(HttpContext context)
{
context.Response.Headers.Add("x-gamecentral-received", "game1");
return Task.CompletedTask;
}

Am I doing something wrong?

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<https://github.com/CarterCommunity/Carter/issues/209?email_source=notifications&email_token=AAAZVJVLDYZBADHADPJ3Y73QQHVEPA5CNFSM4JEYQDD2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4HUGKMSA>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAAZVJTRRPPUCFOX7CM3H4LQQHVEPANCNFSM4JEYQDDQ>
.
Matias-Barrios commented 4 years ago

Thanks for your response. Now my code changed to this :

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            app.UseExceptionHandler("/errorhandler");
            app.UseCarter(new CarterOptions(before: ctx => HeaderInjector(ctx)));

        }
        private Task<bool> HeaderInjector(HttpContext ctx)
        {
            ctx.Request.Headers["x-gamecentral-received"] = "game1";
            return Task.FromResult(true);
        }

And I don't get any errors anymore. But I don't see the header on the response. I have tested this in a simple endpoint that basically replies with "Healthy!"

jchannon commented 4 years ago

You've set the request headers not the response ;)

On Thu, 24 Oct 2019 at 20:14, Matias.Barrios notifications@github.com wrote:

Thanks for your response. Now my code changed to this :

public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseExceptionHandler("/errorhandler"); app.UseCarter(new CarterOptions(before: ctx => HeaderInjector(ctx)));

    }
    private Task<bool> HeaderInjector(HttpContext ctx)
    {
        ctx.Request.Headers["x-gamecentral-received"] = "game1";
        return Task.FromResult(true);
    }

And I don't get any errors anymore. But I don't see the header on the response. I have tested this in a simple endpoint that basically replies with "Healthy!"

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/CarterCommunity/Carter/issues/209?email_source=notifications&email_token=AAAZVJWQOQOONDXF3KHI7CTQQHXZDA5CNFSM4JEYQDD2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECGD62Y#issuecomment-546062187, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAZVJVCOFLZHM66CDTFRBTQQHXZDANCNFSM4JEYQDDQ .

Matias-Barrios commented 4 years ago

@jchannon Thanks so much! Now its working!

jchannon commented 4 years ago

No probs :)

On Thu, 24 Oct 2019 at 20:28, Matias.Barrios notifications@github.com wrote:

Closed #209 https://github.com/CarterCommunity/Carter/issues/209.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/CarterCommunity/Carter/issues/209?email_source=notifications&email_token=AAAZVJWQUDBM7IEXPNLU3GDQQHZPNA5CNFSM4JEYQDD2YY3PNVWWK3TUL52HS4DFWZEXG43VMVCXMZLOORHG65DJMZUWGYLUNFXW5KTDN5WW2ZLOORPWSZGOUNXN7LA#event-2741952428, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAZVJXMYF4JX35PND2EBQDQQHZPNANCNFSM4JEYQDDQ .