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
The shortest and most performant path is to perform the null check against every level. For reuse you can wrap that code up into a helper function or perhaps an extension method. This will let you safely access it via that function, but still consistently perform the null check.
Example:
public void DoSomething()
{
// Get the path, which may be null, using the extension method
var contextPath = HttpContext.Current.RequestPath;
}
public static class HttpContextExtensions
{
public static string RequestPath(this HttpContext context)
{
if (context == null || context.Request == null)
{
return default(string);
}
return context.Request.ApplicationPath;
}
}