find a location for property in a new city

Wednesday, 16 March 2011

WCF REST: How to get a Request header

Again WCF REST is proving secretive when it comes to getting at your Request Headers.

You can get at any custom Request headers that you may be expecting via the IncomingMessageProperties off of the OperationContext in the same way as before when you were adding custom Headers to your response.

To do this you you can write something like this:
var request = OperationContext.Current.IncomingMessageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
var version = request.Headers["ApiVersion"];

Update:

Actually, there is an easier way I found. Since this is in a REST service it is possible to use WebOperationContext which seems a lot more intuitive and concise:
var version = WebOperationContext.Current.IncomingRequest.Headers["ApiVersion"];

You will then have access to any expected Request Headers as a string.

More WCF REST:

Follow britishdev on Twitter

15 comments:

  1. You rock!
    For others trying this same thing, sending information from an asp.net program is as easy as adding a .Add:
    wc.Encoding = System.Text.Encoding.UTF8;
    wc.Headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET4.0C; .NET4.0E)";
    wc.Headers["Content-Type"] = "application/json";
    wc.Headers.Add("myCustomHeader", "Custom Value");
    dynamic a = wc.UploadString(new Uri(url), data);

    On the server side:
    string glub = System.ServiceModel.Web.WebOperationContext.Current.IncomingRequest.Headers["myCustomHeader"];

    Thank you so much!

    ReplyDelete
    Replies
    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from .Net Core Training in Chennai. or learn thru .Net Core Training in Chennai. Nowadays Dot Net has tons of job opportunities on various vertical industry.
      or Es6 Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

      Delete
  2. Thank you, it was very useful to me

    ReplyDelete
  3. Motifz Designer Lawn. 1, 2 & 3 Piece Unstitched Premium Embroidered Lawn 2019, Premium embroidered Lawn, Premium lawn, Premium lawn 2019, Motifz, Premium lawn in Pakistan, Summer collection. Shipping worldwide. Stitching option available.

    ReplyDelete
  4. This is my first visit to your web journal! We are a group of volunteers and new activities in the same specialty. Website gave us helpful data to work. SubZero, Viking Ice Maker Repair Near Me

    ReplyDelete
  5. Great Article
    Cloud Computing Projects




    Networking Projects



    Final Year

    Projects for CSE





    JavaScript Training in Chennai



    JavaScript

    Training in Chennai




    The Angular Training covers a wide range of topics including Components, Angular Directives, Angular Services, Pipes, security fundamentals,

    Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer.

    Angular Training

    ReplyDelete
  6. Shop for Women’s best hair removal epilators and electric tweezers in Pakistan from all imported and local brands at Al-Fatah. We have extensive range of brands of hair removal epilators in Pakistan. Our variety of hair removal epilators and electric tweezers is extensive. Shop for Epilators at Al-Fatah now.

    ReplyDelete
  7. Do you need Quick Loan to settle your bills and startup
    business? contact us now with your details to get a good
    Loan at a low rate of 2% per Annual
    Do you need Personal Finance?
    Business Cash Finance?
    Unsecured Finance
    Fast and Simple Finance?
    Quick Application Process?
    Finance. Services Rendered include,
    *Debt Consolidation Finance
    *Business Finance Services
    *Personal Finance services Help
    contact us for more information on how to get started:
    (Whats App) number:+919394133968
    please contact email id : patialalegitimate515@gmail.com
    Mr Sorina Jeffery

    ReplyDelete
  8. I learned a lot from the insight you shared here. It's good to learn more about this topic, and if you have some free time or you're curious about some celebrity basic information, you can visit celebrity heights and search for it.

    ReplyDelete
  9. Buy tank tops for men online from Dshred at easy prices delivered at your doorstep. Get free shipping

    ReplyDelete