Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1014267
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:13:40+00:00 2026-05-16T10:13:40+00:00

Very simple I’m sure, but driving me up the wall! There is a component

  • 0

Very simple I’m sure, but driving me up the wall! There is a component that I use in my web application that identifies itself during a web request by adding the header “XYZComponent=true” – the problem I’m having is, how do you check for this in your view?

The following wont work:

if (Request.Headers["XYZComponent"].Count() > 0)

Nor this:

if (Request.Headers.AllKeys.Where(k => k == "XYZComponent").Count() > 0)

Both throw exceptions if the header variable has not been set. Any help would be most appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:13:40+00:00Added an answer on May 16, 2026 at 10:13 am
    if (Request.Headers["XYZComponent"].Count() > 0)
    

    … will attempted to count the number of characters in the returned string, but if the header doesn’t exist it will return NULL, hence why it’s throwing an exception. Your second example effectively does the same thing, it will search through the collection of Headers and return NULL if it doesn’t exist, which you then attempt to count the number of characters on:

    Use this instead:

    if(Request.Headers["XYZComponent"] != null)
    

    Or if you want to treat blank or empty strings as not set then use:

    if((Request.Headers["XYZComponent"] ?? "").Trim().Length > 0)
    

    The Null Coalesce operator ?? will return a blank string if the header is null, stopping it throwing a NullReferenceException.

    A variation of your second attempt will also work:

    if (Request.Headers.AllKeys.Any(k => string.Equals(k, "XYZComponent")))
    

    Edit: Sorry didn’t realise you were explicitly checking for the value true:

    bool isSet = Boolean.TryParse(Request.Headers["XYZComponent"], out isSet) && isSet;
    

    Will return false if Header value is false, or if Header has not been set or if Header is any other value other than true or false. Will return true is the Header value is the string ‘true’

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Very simple question but all the answer I read over the web doesn't apply.
Very simple Winforms application I want to deploy manually. Can all the referenced assemblies
Very simple Qt GUI application: On the scene I have multiple circles implemented as
Very simple questions guys, but maybe I'm just forgetting something. In 64bit linux, a
Very simple question, but I want to start using a consistent naming convention for
Very simple question but giving me hard time, I want to replace to \
Very simple I guess but I cannot get what I perceive to be the
Very simple problem...but weird results. Im just trying to fill a drop down list
Very simple problem, but can't seem to solve it. I'm probably just not thinking
Very simple question I am sure. I have a file called ACLReader.vbs which I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.