string extension method for Session["key"]
At times when we access Session["key"].ToString()
it gives exception when session is expired & in coding we try to access session variable. So I'm t开发者_如何学运维rying to create extension method on object
class so that I could write it as Session["key"].getString()
in coding so that every time I don't have to do Convert.ToString(session["key"])
Any other solutions are also appreciable.
Just use the null-coalescing operator:
string value = (session["key"] ?? String.Empty).ToString();
Update
If you must have a method to do this (extension or otherwise), I would do something like:
public static string GetValue(this HttpSessionState session, string key)
{
// TODO: Insert appropriate error checking here.
return (session[key] ?? String.Empty).ToString();
}
I might even go so far as to make it generic for other possible types with a GetValue
call that takes a selector and then use lambdas:
public static T GetValue<T>(this HttpSessionState session, string key, Func<object, T> valueSelector)
{
return valueSelector(session[key]);
}
public static string GetStringValue(this HttpSessionState session, string key)
{
return session.GetValue(key, x => (x ?? String.Empty).ToString());
}
You would then use as follows:
string value = session.GetStringValue("key");
public static class ObjectExtensions
{
public static string GetString(this object o)
{
if (o == null)
{
return string.Empty;
}
return Convert.ToString(o);
}
}
and then:
string value = Session["key"].GetString();
or check this one:
public static class SessionExtensions
{
public static string GetString(this HttpSessionStateBase session, string key)
{
if (session == null)
{
return string.Empty;
}
var value = session[key];
if (value == null)
{
return string.Empty;
}
return Convert.ToString(value);
}
}
and then:
string value = Session.GetString("key");
Maybe this can ba an alternative?
object oKey = session["key"] ?? String.Empty;
string sKey = (string)oKey;
or
string sKey = session["key"] == null ? String.Empty : (string)session["key"]
public static class ObjectExtensions
{
public static string SafelyToString(this object o)
{
return o != null ? o.ToString() : string.Empty;
}
}
This will allow Session["key"].SafelyToString()
You will not however be able to discern between an empty string in the session variable and an expired session.
I would also advise using properties for that.
protected YourType PropertyName
{
get
{
if(Session["Sessionname"] != null)
{
return Session["Sessionname"] as YourType;
}
YourType newItem = new YourType();
// set vars
Session["Sessionname"] = newItem;
return newItem;
}
set
{
Session["Sessionname"] = value;
}
}
As you can see, I chose protected
as access modifier. If you want you could put it in a public class
and make the property static
.
Or you can use class that derives from System.Web.UI.Page
.
public class MyCustomBaseClass : System.Web.UI.Page
{
protected YourType PropertyName
{
// get and set like above
}
}
Now you can replace the inheritance in your content pages from System.Web.UI.Page
to MyCustomBaseClass
and can easily call this.PropertyName
.
Here is a easy way to access Session in Extension method:
var loggedUser = (User)System.Web.HttpContext.Current.Session["User"];
精彩评论