ASP常用内置对象

ASP 常用内置对象

  • *个人学习笔记留档
  • 感谢教程 https://www.bilibili.com/video/BV1mt41177NZ

内置对象

Request 请求

服务器接受客户端请求

Request.QueryString Get请求
        public ActionResult Index()
        {
            return Content($"{Request.QueryString["name"]}--{Request.QueryString["age"]}--{Request.QueryString["id"]}");
        }
Request.Form Post请求
        public ActionResult PostData()
        {
            return Content(Request.Form["loginname"]);
        }
Request.MapPath() 将虚拟路径转换为物理路径
Request.Files Post请求上传的文件
        public ActionResult FileData()
        {
            //SaveAs方法需要物理路径
            //物理路径:绝对路径
            //虚拟路径:相对路径,
            Request.Files["file"].SaveAs(Request.MapPath("~/uploads/" + Request.Files["file"].FileName));
            return Content("OK");
        }
Request.Headers 请求头信息
        public ActionResult RequestHander()
        {
            //Headers
            //主体以外的头消息      
            Response.Headers["HELLO"] = "world";//设置响应消息的头消息
            return Content(Request.Headers["token"]);//返回请求头消息中键"token"的值
        }

Resopnse 响应

服务器给客户端的结果都称为响应

Response.Write() 向客户端输出内容
				Response.Write("Hello World!"); 
Response.Redirect(url)重定向,更改响应页面
            Response.Redirect("https://translate.google.cn/");//重定向到谷歌翻译

Session 会话

Session 会话数据保存在服务器中,占用服务器资源

Session 是一个键值对

每个客户端的Session独立

大多只存储少量重要数据,如登录账号信息

只存活20min 无操纵20min后自动销毁

创建Session
        public ActionResult SessionData()
        {
            //Session 销毁 Abandon/Clear
            Session["user"] = Request.Form["user"];
            Session["id"] = Request.Form["id"];
            return Content("Session['user']中的数据是:" + Session["user"]);
        }
销毁Session
        public ActionResult ClearSession()
        {
            //Session.Clear();
            Session.Abandon();
            string struser = "Session[user]状态:";
            if (Session["user"] == null)
            {
                struser += "已销毁";
            }
            else
            {
                struser += "未销毁";
            }
            return Content(struser + "Session[id]:" + Session["id"]);
        }

Cookie 客户端数据

Cookie 客户端(浏览器)储存的数据

一概认为是不安全数据

具有时效性

将Cookie响应给客户端
        public ActionResult CookieSave()
        {
            //通过Response的方式将Cookie响应给客户端,浏览器自动存储
            Response.Cookies.Add(new HttpCookie("token")
            {
                Value = "abc123sd",
                Expires = DateTime.Now.AddDays(7)//Cookie存储时间
            });
            return Content(Request.Cookies["token"].Value);
        }
读取Cookie
        public ActionResult GetCookies()
        {
            return Content(Request.Cookies["token"].Value);
        }
清除Cookie
        public ActionResult ClearCookie()
        {
            //用过期的方式清除Cookie的特定值
            Response.Cookies.Add(new HttpCookie("token")
            {
                Expires = DateTime.Now.AddDays(-1)
            });
            return Content("OK");
        }

Application 当前网站对象

Application 是解决方案共有–Session每个人相互独立

存储Application
        public ActionResult ApplicationData()
        {
            HttpContext.Application["vvv"] = "123";
            return Content("");
        }
获取Application
        //解决方案共有(多个浏览器访问都能获取到123),而Session切换浏览器则不行
        public ActionResult GetApplication()
        {
            return Content(HttpContext.Application["vvv"].ToString());
        }

Server 服务器对象

包含了服务器里的常用方法

Server.MapPath() 虚拟路径转实际路径
Server.MachineName 服务器名
Server.ScriptTimeout Script超时时间
Server.Transfer() 转发

重定向路径会变

转发路径不变 内容发生变化

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值