ASP常用内置对象(ASP common built-in objects)

内置对象分别是 :Request Response Session Cookie Application Server

Application 和session的区别:session每个人相互独立 Application整个项目都可以调用

1、Application对象:

可以使用Application对象使给定应用程序的所有用户共享信息。

Application存值的方式是:HttpContext.Application[“命名”]=value

   HttpContext.Application["user"] = "123";

Application取值的方式是:HttpContext.Application[“命名”].ToString()

HttpContext.Application["user"].ToString()

2、Request对象:

可以使用Request对象访问任何用HTTP请求传递的信息,包括从HTML表格用POST方法或GET方法传递的参数。

通过Request.QueryString 获取的是Get传值 就是通过Url传值 这种传值方式不安全

通过Request.Form获取的是post传值 大部分传值方式都是通过这种方式传值相对于Get传值这种更安全

Request.Files 通过post请求的文件(文件上传) 上传文件的时候要进行路径转换即虚拟路径转换成物理路径

例:

*//控制器里面写的方法 public ActionResult FileData()
        {                //SaveAs方法需要物理路径 Request.MapPath()将虚拟路径转化成物理路径
            Request.Files["file"].SaveAs(filename:Request.MapPath("~/Upload/"+Request.Files["file"].FileName));
            return Content("ok");
        }
//from表单里面的内容  注:method必须是post方式 enctype="multipart/form-data"必不可少
<form action="/Home/FileData" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <button>提交</button> </form>

3、Response对象

可以使用Response对象控制发送给用户的信息。

Response.Write 向客户端输入内容

Response.Redirect 重定向请求路径

4、Server对象

Server对象提供对服务器上的方法和属性进行的访问。

server提供的方法:

1.Server.Transfer 转发 路径不变 内容改变 注意:只能转发本项目

例:

public ActionResult ServerDemo() {

           //本项目的一个网页Server.Transfer(path:"/WebForm1.aspx");
            return Content("");  }

2. server.MapPath  虚拟路径转换成物理路径

3.内容转码  :server.HtmlEncode   server.HtmlDecode  server.UrlEncode server.HtmlDecode

5、Session对象

可以使用Session对象存储特定的用户会话所需的信息。Session会话 数据保存在服务器中储存少量重要数据 , session是一个键值对。  操作默认有效时间20分钟 ,session是单独的不互通。

消除session的方法是:Session.Abandon();清除数据

6、Cookie对象

cookie主要用来做登录的状态保持或者非重要的数据缓存,Cookie储存客户端;每一个浏览器都有它自己独立的cookie, 由于其他软件的干预显得不安全,Cookie具有时效性。

储存Cookie的方法是.Cookies.Add

例:

 Response.Cookies.Add(new HttpCookie(name: "token")
            {
                Value = "abc123321",
                //存储时间
                Expires = DateTime.Now.AddDays(7)
            }) ;

获取Cookie的方法是: Request.Cookies[“token”].Value

清除Cookie的方法是:使用过期的时间

 public ActionResult CookieClear()
        {
            Response.Cookies.Add(new HttpCookie(name: "token")
            {
                //存储过期的时间
                Expires = DateTime.Now.AddDays(-1)
            });
————————

内置对象分别是 :Request Response Session Cookie Application Server

The difference between application and session: session everyone is independent of each other, and the entire application project can be called

1. Application object:

You can use the application object to enable all users of a given application to share information.

Application存值的方式是:HttpContext.Application[“命名”]=value

   HttpContext.Application["user"] = "123";

Application取值的方式是:HttpContext.Application[“命名”].ToString()

HttpContext.Application["user"].ToString()

2. Request object:

You can use the request object to access any information passed by HTTP request, including parameters passed by post method or get method from HTML table.

Get is obtained through request.querystring, and the value is passed through URL, which is unsafe

Post value transfer is obtained through request.form. Most value transfer methods are through this method, which is safer than get value transfer

Request.files when uploading files through the file requested by post (file upload), path conversion is required, that is, virtual path is converted into physical path

Example:

*//控制器里面写的方法 public ActionResult FileData()
        {                //SaveAs方法需要物理路径 Request.MapPath()将虚拟路径转化成物理路径
            Request.Files["file"].SaveAs(filename:Request.MapPath("~/Upload/"+Request.Files["file"].FileName));
            return Content("ok");
        }
//from表单里面的内容  注:method必须是post方式 enctype="multipart/form-data"必不可少
<form action="/Home/FileData" method="post" enctype="multipart/form-data"> <input type="file" name="file" /> <button>提交</button> </form>

3. Response object

You can use the response object to control the information sent to the user.

Response.write inputs to the client

Response.redirect redirect request path

4. Server object

The server object provides access to methods and properties on the server.

Methods provided by server:

1. Server.transfer forwarding   The path remains unchanged and the content changes. Note: you can only forward this project

Example:

public ActionResult ServerDemo() {

           //本项目的一个网页Server.Transfer(path:"/WebForm1.aspx");
            return Content("");  }

two   server.MapPath   Convert virtual path to physical path

3.内容转码  :server.HtmlEncode   server.HtmlDecode  server.UrlEncode server.HtmlDecode

5. Session object

You can use the session object to store the information required for a specific user session. Session data is saved in the server. A small amount of important data is stored. Session is a key value pair.   The default effective time of the operation is 20 minutes. The session is separate and does not communicate.

The method to eliminate session is: session. Abandon(); Clear data

6. Cookie object

Cookies are mainly used to maintain login status or cache unimportant data. Cookies are stored on the client; Each browser has its own independent cookie. Due to the intervention of other software, cookies are unsafe and time-consuming.

The method to store cookies is. Cookies. Add

Example:

 Response.Cookies.Add(new HttpCookie(name: "token")
            {
                Value = "abc123321",
                //存储时间
                Expires = DateTime.Now.AddDays(7)
            }) ;

获取Cookie的方法是: Request.Cookies[“token”].Value

The way to clear cookies is to use the expired time

 public ActionResult CookieClear()
        {
            Response.Cookies.Add(new HttpCookie(name: "token")
            {
                //存储过期的时间
                Expires = DateTime.Now.AddDays(-1)
            });