Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
目前我有代码
var req = HttpContext.Current.Request; if(!isNull(req["title"], req["desc"], req["tags"])) { doSomthing();}
在某些情况下,我将标题移动到会话数据中,然后重定向页面或做任何我需要的事情。现在这不起作用。有什么我可以用来从请求或会话中提取数据的东西吗?
怎么样:
var ctx = HttpContext.Current; object val = ctx.Request[key] ?? ctx.Session[key];
??是空合并运算符,并获取第一个非空结果(当它有一个时短路),如果没有非空结果,则为空。
??
使用 C# 3.0 您还可以添加扩展方法:
static object GetFromAny(this HttpContext ctx, string key) { return ctx.Request[key] ?? ctx.Session[key]; }