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. Postman 测试 Web Service 和 WCF

    一.postman 测试web service(1)设置url http://www.oorsprong.org/websamples.countryinfo/countryinfoservice.w ...

  2. Pytorch实战学习(七):高级CNN

    <PyTorch深度学习实践>完结合集_哔哩哔哩_bilibili Advanced CNN 一.GoogLeNet Inception Module:而为了减少代码的冗余,将由(卷积(C ...

  3. 牛客算法进阶——树形dp

    1. 小G有一个大树(求树的重心) 删除该点后最大连通块的节点数最小 设f[x]表示以x为根的子树大小,那么删除x之后的各子树大小为f[to]和n-f[x] 求max(max(f[to]),n-f[x ...

  4. SPI主机Verilog代码实现

    前面已经提到过了SPI,在SPI从机的设计中已经讲过SPI的基本原理,这里就不再赘述.对于SPI的主机可以参考百度百科或则笔者前面写的SPI从机介绍的相关知识. 下面是SPI_master的代码 SP ...

  5. JMeter创建上传文件脚本

    环境:Macbook10 ,apache-jmeter-4.0 1. 创建脚本:添加 -> Sampler -> HTTP请求 2.基本配置 3. 设置实现方式,这一步是关键的关键.... ...

  6. mysql开启root用户远程管理权限

    来源:https://blog.csdn.net/qq_29670375/article/details/120590041 1.使用"mysql -uroot -proot"命令 ...

  7. uniapp改变icon

    <!DOCTYPE html><html lang="zh-CN"> <head> <meta charset="utf-8&q ...

  8. [django]钩子函数的一些细节(clean)

    函数名 说明:clean_后面跟着的是需要校验字段名称 示例: class RelUserReset(forms.ModelForm): def clean_confirm_password(self ...

  9. Linux LVM分区相关知识

    Linux分区有多种方式,一种是LVM格式的比较方便,另一种是标准分区扩容比较麻烦,麻烦的事情那么出错的概率也就越大,所以建议生产环境上分区都使用LVM格式硬盘分区. 一.    什么叫LVM?  L ...

  10. CANas分析软件,DBC文件解析,CAN报文分析,仿CANoe曲线显示

    2023.01.01:增加对Kvaser的支持参考了CANoe写了下面的软件,主要用途是对报文的回放及曲线的分析. 1.CAN连接,支持周立功CAN.CANFD及PCAN 2.DBC解析与生成文件 打 ...