【问题标题】:use sessions in web service在 Web 服务中使用会话
【发布时间】:2016-11-08 07:20:33
【问题描述】:

我正在 mvc5 中创建一个 web 应用程序,在其中我通过 web 服务中的会话传递我的登录信息

        if (pm != null)
        {
            string pm1 = pm.ToString();
            HttpContext.Current.Session["pm"] = pm1.ToString();
        }

如果我的string pmnot null,那么字符串 pm 的值将存储在我的会话中,但是当我运行我的网络服务时出现错误

System.NullReferenceException:对象引用未设置为实例 一个对象。

还有其他方法吗?

【问题讨论】:

  • 从哪里将数据保存到会话中?

标签: web-services session asp.net-mvc-5


【解决方案1】:

为此,您需要在 webmethod 属性中启用会话 然后使用

    String.IsNullOREmpty(pm)

  [webmethod(EnableSession = true)]
   public string  your method name
   {
      if (pm != null)
    {
        string pm1 = pm.ToString();
        HttpContext.Current.Session["pm"] = pm1.ToString();
    }

    }

【讨论】:

    猜你喜欢
    • 2011-09-16
    • 1970-01-01
    • 2012-03-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多