views:

97

answers:

1

currently i have code that does

var req = HttpContext.Current.Request;
if(!isNull(req["title"], req["desc"], req["tags"])) { doSomthing();}

on certain cases i move title into session data then redirect the page or do whatever i need. Now this does not work. Is there something i can use to pull data from either request or session?

+1  A: 

How about:

var ctx = HttpContext.Current;
object val = ctx.Request[key] ?? ctx.Session[key];

?? is the null-coalescing operator, and takes the first non-null result (short-circuiting when it has one), or null if there are no non-null results.

With C# 3.0 You could also add an extension method:

static object GetFromAny(this HttpContext ctx, string key) {
    return ctx.Request[key] ?? ctx.Session[key];
}
Marc Gravell