MVC中的内置对象

MVC中的内置对象
  • request 请求
  • response 响应
  • headers 头
  • session 会话
  • cookie 客户端数据
  • application 当前网站对象
  • server 服务器对象

  • Request
  • ① Request.QueryString : get:请求,通过QueryString的方式 www.baidu,com?name=12&id=10发起

    ② Request.Form: post请求, 通过form的方式发起

    ③ Request.MapPath() 虚拟路径转换为物理路径
    ④Reques.Files() Post请求的文件(文件上传)
    <客户端代码>

     
            
            
        
    

    //服务器代码

      public ActionResult PostFile()
            {
            //虚拟路径转为绝对路径
                Request.Files["file"].SaveAs(Request.MapPath("~/Files/") + Request.Files["file"].FileName);
                return Content(Request.Files["file"].FileName + " is saved");
            }
    
  • Repose 响应 服务器向客户端响应
  • ①Response.Write()向客户端写入内容 ②Response.Redirect() 重定向
  • headers Response.Headers["hi"] = "hello word"; //向客户端发送头 return Content(Request.Headers["token"]); //获取客户端的头
  • Session 会话 所有客户端自己的数据存储空间,存储在服务器,存储少量的数据,账号 Session 是键值对 Session存活时间20min Session 销毁 Clear()/Abandon()
  • Cookie 客户端存储 向客户端添加Cookie
    Response.Cookies.Add(new HttpCookie("username")
    {
             Value = "abc",
              Expires = DateTime.Now.AddHours(1)
     });
    
  • 获取客户端的Cookie
    Request.Cookies[“username”].Value.ToString()
    删除客户端的Cookiex
    Response.Cookies.Add(new HttpCookie(“username”)
    {
    Expires = DateTime.Now.AddDays(-1)
    });

  • Application
  • 全局,键值对形式,整个网站所共有的
    Set:
    HttpContext.Application[“user”] = “123”;

    Get:
    HttpContext.Application[“user”].ToString();

  • Server 常用的方法 Server.MapPath()获取物理路径 和 Request.MapPath() Sever.Transfer() 转发 ,,,
  • 你可能感兴趣的:(MVC学习part1,MVC)