Row extension method for session ["key"]

Sometimes, when we turn to Session["key"].ToString(), it gives an exception when the session has expired, and in encoding we try to access the session variable. So I am trying to create an extension method in the class objectso that I can write it as Session["key"].getString()in encoding, so that every time I do not need to doConvert.ToString(session["key"])

Any other solutions are also noticeable.

+3
source share
6 answers

Just use the zero-coalescing operator:

string value = (session["key"] ?? String.Empty).ToString();

Update
If you should have a way to do this (extension or other), 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();
}

, GetValue, , 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());
}

:

string value = session.GetStringValue("key");
+4
public static class ObjectExtensions
{
    public static string GetString(this object o)
    {
        if (o == null)
        {
            return string.Empty;
        }
        return Convert.ToString(o);
    }
}

:

string value = Session["key"].GetString();

:

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);
    }
}

:

string value = Session.GetString("key");
+5

, ?

object oKey = session["key"] ?? String.Empty;
string sKey = (string)oKey;

string sKey = session["key"] == null ? String.Empty : (string)session["key"]
0

.

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;
  }
}

, protected . , public class static.

, System.Web.UI.Page.

public class MyCustomBaseClass : System.Web.UI.Page
{
   protected YourType PropertyName
   {
    // get and set like above
    }
}

System.Web.UI.Page MyCustomBaseClass this.PropertyName.

0
source
public static class ObjectExtensions
{
  public static string SafelyToString(this object o)
  {
     return o != null ? o.ToString() : string.Empty;
  }
}

This will allow Session["key"].SafelyToString()

However, you cannot distinguish between an empty string in a session variable and an expired session.

0
source

Here is an easy way to access the Session in Extension method:

var loggedUser = (User)System.Web.HttpContext.Current.Session["User"]; 
0
source

Source: https://habr.com/ru/post/1796522/


All Articles