c# - Adding custom headers in .NET WebAPI 2 hosted by IIS 8.5 -


i wrote webapi application c# works fine while developing when in production, hosted iis 8.5 got problem.

in order enable cors (cross origin resource sharing) in controller, implemented option action:

[httpoptions] [allowanonymous] public httpresponsemessage options() {     httpresponsemessage res = new httpresponsemessage();      res.headers.add("access-control-allow-headers", "user-agent, content-type, accept, x-applicationid, authorization, host, content-length");     res.headers.add("access-control-allow-methods", "post");     res.headers.add("access-control-allow-origin", "*");     res.statuscode = httpstatuscode.ok;     return res; } 

as wrote before works ok on visual studio in production, when make options request using fiddler, answer always:

http/1.1 200 ok allow: options, trace, get, head, post server: microsoft-iis/8.5 public: options, trace, get, head, post x-powered-by: asp.net date: fri, 05 feb 2016 16:56:20 gmt content-length: 0 proxy-connection: keep-alive 

i know possible add header's key statically in iis but, in controller, need add custome header dynamic values that:

res.headers.add("x-app-limit-remaining", getremainingcalls()); 

anybody knows how overwrite/change http headers c# web api hosted iis 8. ?

many thanks,

luke

you can tap request pipeline using system.net.http.delegatinghandler

reference: http message handlers in asp.net web api

you can add custom handlers pipeline. message handlers cross-cutting concerns operate @ level of http messages (rather controller actions). example, message handler might:

  • read or modify request headers.
  • add response header responses.
  • validate requests before reach controller.

using rate limit example here simplified handler:

public class webapiratelimithandler : delegatinghandler {      //override sendasync method tap request pipeline     protected override task<httpresponsemessage> sendasync(httprequestmessage request, cancellationtoken cancellationtoken) {         debug.writeline("process request");         // call inner handler.         task<httpresponsemessage> response = base.sendasync(request, cancellationtoken);         debug.writeline("process response");         return response.continuewith(task => {             var httpresponse = task.result;             httpresponse.headers.add("x-app-limit-remaining", getremainingcalls());             return httpresponse;         });             } } 

adding handler pipeline

message handlers called in same order appear in messagehandlers collection. because nested, response message travels in other direction. is, last handler first response message.

public static class webapiconfig {     public static void register(httpconfiguration config) {         //order handlers added important. first in last out         config.messagehandlers.add(new webapiratelimithandler());         config.messagehandlers.add(new someothermessagehandler());          // other code not shown...     } } 

there x-http-method-override example. checkout referenced link more examples.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

dataset - MPAndroidchart returning no chart Data available -

post - imageshack API cURL -