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 8025159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:04:30+00:00 2026-06-04T23:04:30+00:00

I want to do this var path = HttpContext.Current.Request.ApplicationPath; If any of the Properties

  • 0

I want to do this

var path = HttpContext.Current.Request.ApplicationPath;

If any of the Properties along the way is null, i want path to be null, or “” would be better.

Is there an elegant way to do this without Ternaries?

ideally i would like this behavior (without the horrible performance and ugliness)
string path;

try
{
    path = HttpContext.Current.Request.ApplicationPath;
}
catch
{
    path = null;
}

Thank you

  • 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-06-04T23:04:32+00:00Added an answer on June 4, 2026 at 11:04 pm

    [EDIT]

    C# 6 got released a while ago and it shipped with null-propagating operator ?., which would simplify your case to:

    var path = HttpContext?.Current?.Request?.ApplicationPath
    

    For historical reasons, answer for previous language versions can be found below.


    I guess you’re looking for Groovy’s safe dereferencing operator ?., and you’re not the first.
    From the linked topic, the solution I personally like best is this one (that one looks quite nice too).
    Then you can just do:

    var path = HttpContext.IfNotNull(x => x.Current).IfNotNull(x => x.Request).IfNotNull(x => x.ApplicationPath);
    

    You can always shorten the function name a little bit. This will return null if any of the objects in the expression is null, ApplicationPath otherwise. For value types, you’d have to perform one null check at the end. Anyway, there’s no other way so far, unless you want to check against null on every level.

    Here’s the extension method used above:

        public static class Extensions
        {
        // safe null-check.
        public static TOut IfNotNull<TIn, TOut>(this TIn v, Func<TIn, TOut> f) 
                where TIn : class  
                where TOut: class 
                { 
                        if (v == null) return null; 
                        return f(v); 
                }       
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Want to play a file that on path like this /var/spool/sound/dog.wav present on server.
I want to load uimage with image in path like this /var/mobile/Applications/429E283A-6E89-4BCE-861F-252C5327F711/Library/Application Support/Students/43y. F./1.2.840.113564.3.1.2.180.0.0.90.20081228203734483950/1.2.840.113564.10.1.226230117146661685815524478531105858244/Thumbnail.bmp
My current method is this: var request = require('request'); var mime = require('mime'); var
let say I want to do this: var dashboard = {}; var page =
I have a case where I want to do this: var els = {
I want to execute a query like this var result = from entry in
I have an ordered list like this: var list = new List<String>(){aaa,aab,aac,baa,bab,bac}; I want
Basically, I'm doing this: var phone = $(#phone).val(); Then I want to check if:
I want to be able to parse file paths like this one: /var/www/index.(htm|html|php|shtml) into
I have this string: var someString = 23/03/2012; and want to replace all the

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.