正式学习MVC 02
1、cookie
继续讲解MVC的内置对象cookie
相对不安全
1)保存cookie
public ActionResult Index()
{
// 设置cookie以及过期时间
Response.Cookies.Add(new HttpCookie(name: "userId")
{
Value = "128idn62dx",
Expires = DateTime.Now.AddDays()
});
return Content("ok!");
}
2) 获取cookie
public ActionResult Index()
{ return Content(Request.Cookies["userId"].Value);
}
3)移除cookie
public ActionResult Index()
{
// 设置cookie以及过期时间
Response.Cookies.Add(new HttpCookie(name: "userId")
{
Expires = DateTime.Now.AddDays(-)
});
return Content("ok!");
}
2、Application
1) 是全局的
设置application
public ActionResult Index()
{
HttpContext.Application["user"] = "Linda";
return Content(HttpContext.Application["user"].ToString());
}
3、Server
包含了服务器的常用方法
public ActionResult Index()
{
Server.Transfer(path: "html页地址");
return Content("ok");
}
路径不变,内容改变
.MapPath 虚拟路径转物理路径
2、控制器(controller)与视图(view)的数据通信
Controller里面每个方法都可供访问
1) Controller => view
①ViewBag
控制器文件
public ActionResult Index()
{
ViewBag.Info = "info from Controller";
return View();
}
视图文件
@{
ViewBag.Title = "Index";
}
<h2>App Page for DemoController</h2>
<!--访问Controller内数据-->
<p>@ViewBag.Info</p>
②ViewData
public ActionResult Index()
{
ViewData["Name"] = "fiona";
return View();
}
@{
ViewBag.Title = "Index";
}
<h2>App Page for DemoController</h2>
<!--访问Controller内数据-->
<p>@ViewData["Name"]</p>
③TempData
可跨页面传递数据,仅能被访问一次,之后会被清除
public ActionResult Index()
{
TempData["token"] = "23vf5c";
return View();
}
@{
ViewBag.Title = "Index";
}
<h2>App Page for DemoController</h2>
<!--访问Controller内数据-->
<p>@TempData["token"]</p>
上面3中方法传递的都是不主要的数据
主要的数据通过下面的方法传递
④通过View方法传递
@{
ViewBag.Title = "Index";
}
<h2>App Page for DemoController</h2>
<!--访问Controller内数据-->
<p>@Model.Name</p>
<p>@Model.Sex</p>
public ActionResult Index()
{
return View(new Animal()
{
Name = "cat",
Sex = "male"
});
}
ide不能进行识别来提示,可声明
@{
ViewBag.Title = "Index";
}
@model MVCStudy.Models.Animal
<h2>App Page for DemoController</h2>
<!--访问Controller内数据-->
<p>@Model.Name</p>
<p>@Model.Sex</p>
Model内的类型要与View方法参数内的一致
其它方式:指定视图页并传参
public ActionResult Index()
{
return View("ShowData",new Animal()
{
Name = "cat",
Sex = "male"
});
}
@{
Page.Title = "此处显示标题";
//Layout = "此处显示你的布局页";
}
@model MVCStudy.Models.Animal
<div>
this iss ShowData page
</div>
<div> data from democontroller</div>
<p>@Model.Name</p>
<p>@Model.Sex</p>
同时指定布局模板,下面的mylayout位于shared目录下
public ActionResult Index()
{
// 视图名,模板页,数据
return View("ShowData",masterName:"_MyLayout",new Animal()
{
Name = "cat",
Sex = "male"
});
}
------------恢复内容结束------------
正式学习MVC 02的更多相关文章
- 正式学习MVC 01
1.新建项目 点击创建新项目,选择ASP.NET web应用程序,对项目进行命名后点击创建. 截图如下: 取消勾选HTTPS配置 可选择空 + mvc 或直接选定MVC 2.目录结构分析 1) App ...
- 正式学习MVC 05
1.剃须刀模板razor的使用 1)混编 循环语法 @model List<MVCStudy.Models.Student> @{ ViewBag.Title = "List&q ...
- 正式学习MVC 06
1.Model常用属性讲解 using System; using System.ComponentModel.DataAnnotations; namespace MVCStudy2.Models ...
- 正式学习MVC 04
1.ActionResult ActionResult是一个父类, 子类包括了我们熟知的 ViewResult 返回相应的视图 ContentResult 返回字符串 RedirectResult( ...
- 正式学习MVC 03
1.View -> Controller的数据通信 1) 通过url查询字符串 public ActionResult Index(string user) { return Content(u ...
- 白话学习MVC(十)View的呈现二
本节将接着<白话学习MVC(九)View的呈现一>来继续对ViewResult的详细执行过程进行分析! 9.ViewResult ViewResult将视图页的内容响应给客户端! 由于Vi ...
- 学习MVC之租房网站(二)-框架搭建及准备工作
在上一篇<学习MVC之租房网站(一)-项目概况>中,确定了UI+Service的“双层”架构,并据此建立了项目 接下来要编写Common类库.配置AdminWeb和FrontWeb 一.编 ...
- [eShopOnContainers 学习系列] - 02 - vs 2017 开发环境配置
[eShopOnContainers 学习系列] - 02 - vs 2017 开发环境配置 https://github.com/dotnet-architecture/eShopOnContain ...
- 软件测试之loadrunner学习笔记-02集合点
loadrunner学习笔记-02集合点 集合点函数可以帮助我们生成有效可控的并发操作.虽然在Controller中多用户负载的Vuser是一起开始运行脚本的,但是由于计算机的串行处理机制,脚本的运行 ...
随机推荐
- 【shell基础】条件测试
例1 判断目录是否存在 #!/usr/bin/bash back_dir=/var/mysql_back if ! test -d $back_dir;then mkdir -p $back_dir ...
- 随机函数rand()的使用方法——C语言
原理: 引用自百度百科: 所需包含的头文件: #include <stdlib.h> rand()函数是按指定的顺序来产生整数,因此每次执行上面的语句都打印相同的两个值,所以说C语言的随机 ...
- springboot支付项目之日志配置
日志框架 本节主要内容: 1:常见的几种日志框架 2:Logback的使用 3:怎么配置info和error级别日志到不同文件中并且按照日期每天一个文件. 以上几个框架可以分类如下: SLF4J和Lo ...
- open 管道用法|Getopt::Long
#!/usr/bin/perl use strict; use warnings; use Getopt::Long; my ($number,$in,$out); GetOptions( " ...
- python之接口开发
一.接口开发的思路 1.启动一个服务: 2.接受客户端传过来的数据: 3.登录,注册,支付等功能 4.操作数据库,拿到数据: 5.返回数据: import flask server=flask.Fla ...
- iOS传感器集锦、飞机大战、开发调试工具、强制更新、Swift仿QQ空间头部等源码
iOS精选源码 飞机大作战 MUPhotoPreview -简单易用的图片浏览器 LLDebugTool是一款针对开发者和测试者的调试工具,它可以帮... 多个UIScrollView.UITable ...
- SPI以及IIC的verilog实现以及两者之间的对比
一.SPI是一种常用的串行通信接口,与UART不同的地方在于.SPI可以同时挂多个从机,但是UART只能点对点的传输数据,此外SPI有四条线实现数据的传输,而UART采用的是2条实现串行数据的传输 1 ...
- [LC] 572. Subtree of Another Tree
Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and no ...
- SCI|EI|ISTP|万方|istic|NSTL|CASTD|CNKI|nlc|ethesys|CALIS|CETD|proquest|NDLTD|中国科学院学位论文检索系统|学位论文
BD AC D 三大检索指的是:SCI(科学引文索引 ).EI(工程索引 ).ISTP(科技会议录索引 ) 即Science Citation Index.Engineering Index.Conf ...
- 视觉SLAM算法框架解析(3) SVO
版权声明:本文为博主原创文章,未经博主允许不得转载. SVO(Semi-direct Visual Odometry)[1]顾名思义是一套视觉里程计(VO)算法.相比于ORB-SLAM,它省去了回环检 ...