MVC内置函数

----HTML页

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
 
<body>
    <form action="/Dome/PostDate" method="post">
 
        <input type="text" name="loginname" />
        <button>提交</button>
    </form>
    <form action="/Dome/FileDate" method="post" enctype="multipart/form-data">
        <input type="file" name="file" />
        <button>提交</button>
    </form>
    <form action="/Dome/SessionDate" method="post">
 
        <input type="text" name="user" />
        <button>提交</button>
    </form>
</body>
</html>

----控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 
namespace 项目.Controllers
{
    public class DomeController : Controller
    {
        // GET: Dome
        public ActionResult Index()
        {
            //内置对象  Request  Response   Session   Cooking      Application    Server
            //          请求     响应        会话     客户端数据    当前网站对象   服务器对象
 
            //Request 服务器接收客户端数据
            //Request.QueryString get 请求!
            //Request.Form post 请求!
            //request.MapPath()作用是将虚拟路径转化为物理路径
            // Request.Files["file"] 获取的是POSt请求的文件,俗称文件上传
 
            //headers
            return Content($"{Request.QueryString["name"]}-{Request.QueryString["age"]}");
             //http://localhost:62036/?name=asdf&age=12  用于接收客户端传来的数据
 
        }
        public ActionResult PostDate()
        {
            return Content(Request.Form["loginname"]);
        }
        public ActionResult FileDate()
        {
            //saveas 方法需要物理路径
            //request.MapPath()作用是将虚拟路径转化为物理路径
            Request.Files["file"].SaveAs(filename: Request.MapPath("~/uploads/" + Request.Files["file"].FileName));
            return Content("ok");
        }
 
        public ActionResult ReqponseDate()  //响应是服务器给客户端
        {
            //   Response.Write 向客户输出内容
            //   Response.Redirect 重定向
            //Response.Write(s:"hello word");  
            //http://localhost:62036/Dome/ReqponseDate 案例
            Response.Redirect("http://www.baidu.com//");
            return Content("");
        }
        public ActionResult RequestHeader()
        {
            Request.Headers["hollow"] = "world";
            return Content(Request.Headers["token"]);  //请求头
        }
        public ActionResult SessionDate() //所有人自己的存储空间
        {
            //Sission 会话,数据保存在服务器中(一般用来存储少量重要数据(用户名))
            //Sission 是一个键值对
            //Session 的存活时间为20分钟
            //Session 销毁 Abandon/Clear
            Session["user"] = Request.Form["user"];
            return Content("会话中使用的数据是"+Session["user"]);
        }
        public ActionResult GetSession()
        {
            return Content("当前会话的数据是"+Session["user"]);
        }
        public ActionResult ClearSession()
        {
            Session.Clear();   //清除Sission
            Session.Abandon(); //销毁Sission
            return Content("Sission已经清除");
        }
 
        //Cookie  客户端输出的数据一概认为是不安全的数据
 
        public ActionResult CookieSave()
        {
            //时效性
            Response.Cookies.Add(new HttpCookie(name: "token")//服务器告诉客户端cookie需要保存7天
            {
                Value = "asdfasdfasd",
                Expires = DateTime.Now.AddDays(7)
            });
            return Content("ok");
        }
        public ActionResult CookieGet()
        {
            return Content(Request.Cookies["token"].Value); //客户端请求获取到cookie的值
            //http://localhost:62036/dome/CookieGet
        }
        public ActionResult CookieClear()
        {
 
            //清除cookie 值,使用过期的方式
            Response.Cookies.Add(new HttpCookie(name: "token")
            {
                Expires = DateTime.Now.AddDays(-1)
            });
            return Content("OK");
        }
        public ActionResult ApplicationDate()  // Application  整个服务器共用存
        {
            HttpContext.Application["user"] = "adsfasdf";
            return Content("");
        }
        public ActionResult ApplicationGet() //Application  整个服务器共用 取
        {
            HttpContext.Application["user"] = "adsfasdf";
            return Content(HttpContext.Application["user"].ToString());
        }
        public ActionResult ServerDate()  //服务器对象的应用
        {
            // Server.Transfer 路径不变,内容变了,但是只能转发当前网址内
            //   Server.MapPath(); 虚拟路径转化为物理路径
            //   Response.Redirect 用于重定向可定向任意路径
            Server.Transfer(path:"`/~~~");
            return Content("辛苦了");
        }
    
    }
}

MVC内置对象的更多相关文章

  1. JSP的执行原理、JSP的内置对象、四大作用域解析、MVC模式理解>从零开始学JAVA系列

    目录 JSP的执行原理.JSP的内置对象.四大作用域解析.MVC模式理解 JSP的执行原理 这里拿一个小例子来解析JSP是如何被访问到的 首先将该项目部署到tomcat,并且通过tomcat启动 通过 ...

  2. [.Net Core] 简单使用 Mvc 内置的 Ioc(续)

    简单使用 Mvc 内置的 Ioc(续) 本文基于 .NET Core 2.0. 上一章<[.Net Core] 简单使用 Mvc 内置的 Ioc>已经对日常 Mvc 中的 Ioc 的简单用 ...

  3. JSP运行过程 JSP脚本 静态动态包含 jsp指令 jsp内置对象jsp四大作用域 jsp动作元素 EL表达式 JSTL 设计模式 JSP开发模式 EL内置对象

    Day38 JSP JSP的运行过程具体如下: (1)客户端发出请求,请求访问JSP文件. (2)JSP容器先将JSP文件转换成一个Java源文件(Java Servlet源程序),在转换过程中,如果 ...

  4. MVC路由 路由的三种扩展 替换MVC内置的Handler

    Global.asax 是 程序入口文件 路由配置   为什么localhost:8088/Home/Index/1 能返问到我们写的 会去掉前缀跟端口号  变成Home/Index/1 用这个跟路由 ...

  5. SpringBoot系列三:SpringBoot基本概念(统一父 pom 管理、SpringBoot 代码测试、启动注解分析、配置访问路径、使用内置对象、项目打包发布)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.了解SpringBoot的基本概念 2.具体内容 在之前所建立的 SpringBoot 项目只是根据官方文档实现的一个基础程 ...

  6. Asp.Net六大内置对象

    前面学习mvc管道处理模型的时候,我们晓的HttpContext是贯穿全文的一个对象,在HttpRuntime产生,现在我们所谓的Asp.Net六大内置对象,其实就是HttpContext的属性.具体 ...

  7. Spring,SpringMVC,MyBatis,Hibernate,Servlet的生命周期,jsp有哪些内置对象,Tomcat,Cookie和Session的作用以及区别,oracle,MYSQL等面试题总结

    1. 什么是Spring,谈谈你对Spring的理解 Spring是我们JAVA开发人员在搭建后台时选用的一个轻量级的开源框架,Spring框架目前也是功能丰富,十分优秀企业级采用最多的一个框架. S ...

  8. java web学习总结(二十五) -------------------JSP中的九个内置对象

    一.JSP运行原理 每个JSP 页面在第一次被访问时,WEB容器都会把请求交给JSP引擎(即一个Java程序)去处理.JSP引擎先将JSP翻译成一个_jspServlet(实质上也是一个servlet ...

  9. javaScript的内置对象

    javaScript 有11种内置对象: Array . String .Date .Math . Boolean .Number . Function .Global .Error . RegExp ...

  10. ASP.NET的六大内置对象

    ASP.NET 六大内置对象(System.Web.UI.Page类): 1.Response 2.Request 3.Server 4.Application 5.Session 6.Cooki R ...

随机推荐

  1. 「postOI」Lost Array

    题意 有一个序列 \(A=\{a_1, a_2, ..., a_n\}\),按如下方式构造一个 \((n + 1) \times (n + 1)\) 的矩阵 \(B\): \(B_{i0}=0\)(\ ...

  2. windows导出当前目录结构

    cd 进入目录 tree /f>>tree.txt

  3. 用requests-html和SelectorGadget轻松精准抓取网页数据

    我们在抓取网页数据时,最常採用Python的requests搭配BeautifulSoup的模式来完成.然而,requests-html整合了上述2个套件,又添加了新的功能,或许是抓取网页数据值得考虑 ...

  4. 微信小程序图片和签名

    图片上传功能 chooseImage(e) { wx.chooseImage({ sizeType: ['original', 'compressed'], //可选择原图或压缩后的图片 source ...

  5. 亲测:一个完整Vue开发环境搭建。

    参考博客飞机: https://www.cnblogs.com/zhaomeizi/p/8483597.html

  6. python 操作 ES 二、mappings

    环境 python:3.8 es:7.8.0 环境安装 pip install elasticsearch==7.8.0 from elasticsearch import Elasticsearch ...

  7. 在输入shell命令的list_namespace时,报 :org.apache.hadoop.hbase.PleaseHoldException: Master is initializing。

    今天弄了一下午这个问题,弄到了将近十点,终于解决了,终于解决这个问题了,感谢旭旭大佬相助,不再报错了. 本来今天中午,我已经弄好了,结果我午睡了一下再看就报错了,哎.今天本来已经绝望了,后来问了一下大 ...

  8. Linux装cudnn

    https://stackoverflow.com/questions/66977227/could-not-load-dynamic-library-libcudnn-so-8-when-runni ...

  9. matplotlib处理数据可是化的时候出现中文显示异常,为[][]的解决方法

    1.在使用matplotlib处理数据实现可是化操作的时候,处于语言习惯,经常会使用中文做图表备注,而中文显示会出现异常,如下图: 中文显示异常 那如何解决呢,实际上我们只需要在代码中添加两行内容即可 ...

  10. iOS基础 - SceneDelegate

    前言 1 - 自从 Xcode11 发布以来,当你使用新 XCode 创建一个新的 iOS 项目时 SceneDelegate 会被默认创建 2 - 在 iOS 13 后 SceneDelegate ...