我在类中加了一句private static readonly System.Web.UI.Page page = new System.Web.UI.Page();
发现静态成员可以通过page访问到Server,Session,Application,这样的效率高吗,可行不可行?
总觉得为了访问Server,Session,Application还要创建一个page对象有点浪费如果我的方法不可行,有什么好的解决方法呢
发现静态成员可以通过page访问到Server,Session,Application,这样的效率高吗,可行不可行?
总觉得为了访问Server,Session,Application还要创建一个page对象有点浪费如果我的方法不可行,有什么好的解决方法呢
HttpRuntime
HttpUtility
参考一下
发现静态成员可以通过page访问到Server,Session,Application,这样的效率高吗,可行不可行? 这样是不可行的,因为你new的Page不是你当前访问的Page实例,明白?方法有2:
1、给你的静态方法添加参数:Page
如:static string test(Page pag){}
在ASPX里调用:test(this);2、使用System.Web.HttpContext.Current
如:System.Web.HttpContext.Current.Server
System.Web.HttpContext.Current.Response
比如
page.Response.Write("aaa");
你看这个aaa能不能输出?
或在类中定义Page对象属性
直接在 HttpContext.Current.Session["test"]就引用了
静态方法前加[WebMethod(EnableSession = true)]注释