.net的内置对象

一 . 获取客户端,服务器端信息:

        Response.Write("客户端信息:");
        Response.Write("<br>浏览器类型,版本:"); Response.Write(Request.Browser.Type);
        Response.Write("<br>浏览器类型:" + Request.Browser.Browser);
        Response.Write("<br>浏览器版本:" + Request.Browser.Version);
        Response.Write("<br>客户端IP地址:" + Request.UserHostAddress);  
        Response.Write("<br>客户端主机名:" + Request.UserHostName);  
        Response.Write("<br>客户端操作系统:" + Request.Browser.Platform); 
        Response.Write("<br>客户系统相关的一些属性:"); Response.Write(Request.ServerVariables["HTTP_USER_AGENT"].ToString());
        Response.Write("<br>浏览器语言:"); Response.Write(Request.ServerVariables["HTTP_ACCEPT_LANGUAGE"].ToString());
        Response.Write("<br><br>服务器端信息:");
        Response.Write("<br>服务器机器名称:" + Server.MachineName);
        Response.Write("<br>网站根路径:" + Server.MapPath("www.baidu.com"));//返回一个字符串,将请求URL中的虚拟路径映射到服务器中的物理路径。

二 .Request
1.属性:

            Request.QueryString["name"]  //获取get方式传递过来的参数
            Request.Params["name"]; //获取由名/值对表示的QueryString,Form,Cookie,ServerVariable组成的集合,效率低
            Request.Url //获取当前请求的URL信息
            Request.MapPath("WebForm1") //获取请求URL中虚拟路径映射在服务器的物理路径
            Request.Browser.Version; //获取客户浏览器相关信息

获取当前页面的所有客户端浏览器端的信息:

    <%
        for (int i = 0; i < Request.ServerVariables.Count; i++)
        {
            Response.Write(Request.ServerVariables.AllKeys[i]+"<br>");
            Response.Write(Request.ServerVariables[i] + "<br><br><br><br>");
        }
         %>

三 . Response
1.属性

   Response.Write();   //输出指定的文本信息
            Response.WriteFile();//输出指定文件信息
            Response.End(); //使WEB服务器停止当前程序并返回结果
            Response.Redirect();//重定向

四 . Cookie

            //创建Cookie
            //方式一
            Response.Cookies["name"].Value = txt.Text;
            Response.Cookies["name"].Expires = DateTime.Now.AddMinutes(20);
            //方式二
            HttpCookie cookie = new HttpCookie("name",txt.Text);
            Response.Cookies.Add(cookie);

   //获取Cookie
   string name=Request.Cookies["name"].Value;

五 . Session

            //写入session
            Session["mySession"] = "张三";
            Session.Timeout = 20;

            //读取Session
            string name = Session["mySession"].ToString();
//配置应用程序配置文件
          <configuration>
              <system.web>
                  <compilation debug="true" targetFramework="4.0" />
                <sessionState timeout="20" cookieless="true" mode="SQLServer"></sessionState>
              </system.web>
          </configuration>

六 . Application
多用于存储少量且不常更改的信息。

常用属性:
All: 返回全部的Application对象变量到一个对象数组。
Allkeys:返回全部的Application对象变量到一个字符串数组。
Count:取得Application对象变量的数值。
Item: 允许使用索引或Application变量名称传回内容值。

常用方法:
Add(): 新增一个Application对象变量。
Clear(): 清除全部Application对象变量。
Get(): 使用索引值或变量名称传回变量值。
Set(): 使用变量名称更改一个Application对象变量的内容。
Lock(): 锁定全部的Application变量。
Unlock(): 解除锁定Application变量。
七 . Server对象
提供对服务器端的访问的方法和属性:
常用方法:

HtmlEncode: 此方法带有一个字符串参数,可将其编码,使其在的浏览器中正确表示,比如希望在页面输出一个"<br>",又不想在浏览器中显示成换行,则需要使用此方法
HtmlDecode: 此方法与上面的方法相反,对已编码的内容解码
MapPath: 此方法带有一个虚拟路径,可返回该 路径在物理磁盘中的准确位置,此方法在Web开发过程中使用频率比较高,一般实现文件读写都需要该方法 用法:Server.MapPath(string path) path参数为Web服务器上的虚拟路径,如果将NULL作为参数,则返回应用程序所在目录的物理路径。 比如返回根据目录下的default.aspx的路径:Server.MapPath("~/defautl.aspx");它会返回该目录的物理路径
UrlEncode: 对URL地址进行编码,对于URL需要传输含有"#","&"等特殊字符的参数时,需要进行编码,否则后面的内容不会被识别
UrlDecode: 与上一方法相反,对URL地址进行解码
Execute: 在当前页面执行参数指定的页面,执行完成后继续执行本页面
Transfer: 参数指定的页面处理完成后,页面执行变结束,不像Execute那样还要返回控制权给先前的页面

posted on 2014-09-18 11:08  逍遥云天  阅读(205)  评论(0编辑  收藏  举报

导航