如何在webmethod()中获取会话变量 [英] How to get session variable in a webmethod()

查看:101
本文介绍了如何在webmethod()中获取会话变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将用户签名保存为UserNameSig.png。

代码有效但是 Session [UserNameSig]。ToString()

我怀疑[WebMethod()]即有问题会话变量。

有谁知道如何解决这个问题。



谢谢



我尝试过:



 [WebMethod()] 
public static void UploadPic (string imageData)
{

string Pic_Path = HttpContext.Current.Server.MapPath(Session [UserNameSig]。ToString()+。png);
using(FileStream fs = new FileStream(Pic_Path,FileMode.Create))
{
using(BinaryWriter bw = new BinaryWriter(fs))
{
byte [ ] data = Convert.FromBase64String(imageData);
bw.Write(data);
bw.Close();
}
}
}

解决方案

我搜索了webmethod session并找到了这些和许多其他人



ASP.Net Web服务 - 如何在Web服务中使用会话状态? [ ^ ]



在Web服务中使用ASP.NET会话状态 [ ^ ]



请自行做基础研究,比如在问问题之前使用谷歌。


Session 是一个实例属性。你无法从静态方法访问它。



相反,你需要使用 HttpContext.Current 来访问它会话:

 HttpContext context = HttpContext.Current; 
string Pic_Path = context.Server.MapPath(context.Session [ UserNameSig]。ToString()+ 。png );


I want to save the user signature as UserNameSig.png.
the code works but Session["UserNameSig"].ToString()
I Suspect that the [WebMethod()] ie having problems with the session variable.
Do anyone know how can solve this.

Thanks

What I have tried:

[WebMethod()]
        public static void UploadPic(string imageData)
        {

            string Pic_Path = HttpContext.Current.Server.MapPath(Session["UserNameSig"].ToString() + ".png");
            using (FileStream fs = new FileStream(Pic_Path, FileMode.Create))
            {
                using (BinaryWriter bw = new BinaryWriter(fs))
                {
                    byte[] data = Convert.FromBase64String(imageData);
                    bw.Write(data);
                    bw.Close();
                }
            }
        }

解决方案

I googled "webmethod session" and found these and many others

ASP.Net Web Services – How to use session state in a web service?[^]

Using ASP.NET Session State in a Web Service[^]

Please do basic research yourself like using google before asking a question.


Session is an instance property. You cannot access it from a static method.

Instead, you will need to use HttpContext.Current to access the session:

HttpContext context = HttpContext.Current;
string Pic_Path = context.Server.MapPath(context.Session["UserNameSig"].ToString() + ".png");


这篇关于如何在webmethod()中获取会话变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆