正式学习MVC 01
1、新建项目
点击创建新项目,选择ASP.NET web应用程序,对项目进行命名后点击创建。
截图如下:
取消勾选HTTPS配置
可选择空 + mvc
或直接选定MVC
2、目录结构分析
1) App_Start
配置文件夹。
BundleConfig.cs
打包器(css,js等)
// ScriptBundle:脚本打包器(导入目录名).includes(所引入目录路径)
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
"~/Scripts/jquery-{version}.js"));
意思是我们把jq引入后重新规定路径为第一个括号内容,之后再在使用#1 括号内内容view内就可以调用了,如下。
通过bundles.Add新增打包设置
// _Layout.cshtml
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
@RenderSection("scripts", required: false)
下一段:
bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
"~/Scripts/jquery.validate*"));
将引入所有以jquery.validate开头的文件
当为相应的控制器创建视图后,选定create等模板,再勾选‘引用脚本库’即可为视图文件引用上述文件
下一段:
// 使用要用于开发和学习的 Modernizr 的开发版本。然后,当你做好
// 生产准备就绪,请使用 https://modernizr.com 上的生成工具仅选择所需的测试。
bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
"~/Scripts/modernizr-*")); bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
"~/Scripts/bootstrap.js"));
下一段引入的是样式文件:
bundles.Add(new StyleBundle("~/Content/css").Include(
"~/Content/bootstrap.css",
"~/Content/site.css"));
}
同时打包了2个样式文件
在模板文件内通过@Styles.Render进行引入
回到web.config文件
system.web标签下compilation标签debug属性
true:调试模式 不打包,引用原始文件
false:发布模式 打包,引用压缩文件
改为false后引用文件路径会有不同
FilterConfig.cs 过滤器:书写过滤规则
RouteConfig.cs 路由配置
2) Content
存放bootstrap css样式等样式文件
3)Contollers
控制器
4)fonts
bootstrap字体(icon)文件
5)Models
数据
6)Scripts
存放js等脚本文件
7)Views
视图
地址栏访问控制器,经由Models数据校验过滤,并最终展现View
每个Controller都会有其对应的View下的文件夹
_ViewStart.cshtml
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
设置Layout模板(布局页页),指定为Shared下的_Layout
也可以填写另一个布局页的路径
如果不使用模板,请声明:
@{
ViewBag.Title = "About";
Layout = null;
}
Shared
_Layout.cshtml 共享视图模板,RenderBody()
2、Controller是如何运行的?
namespace MVCStudy.Controllers
{
public class DemoController : Controller
{
// GET: Demo
public ActionResult Index()
{
return View();
} public ActionResult CreateStudent(Models.Student stu)
{
return View();
}
}
}
1)
通过地址调用相应名称的Controller及其下对应方法
每一个请求否会进入到IIS测试框架的一个管道中,
这个管道会解析地址栏的相关信息,解析出Controller和Action
之后管道自行实例化Controller并调用方法
2)内置对象解析
包括
Request 请求
Response 响应
Session 会话
Cookie 缓存
Application 当前网站对象
Server 服务器对象
①Request
服务器接收到客户端的数据
接收查询字符串(get):
public ActionResult Index()
{
return Content(Request.QueryString["name"]);
}
多个查询字符串
public ActionResult Index()
{
return Content($"{ Request.QueryString["name"]}-{ Request.QueryString["age"]}");
}
获取post请求数据
我们可以使用一个html页提交数据,右键浏览器打开即可:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="/Demo/Index" method="post">
<input type="password" name="password" value="" />
<input type="submit" value="提交" />
</form>
</body>
</html>
Controller内处理:
public ActionResult Index()
{
return Content(Request.Form["password"]);
}
上传文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="/Demo/Index" method="post" enctype="multipart/form-data">
<input type="file" name="file" value="" />
<input type="submit" value="提交" />
</form>
</body>
</html>
public ActionResult Index()
{
// SaveAs方法需要物理路径,而不是虚拟路径,这时可使用MapPath方法
Request.Files["file"].SaveAs(filename:Request.MapPath("~/uploads/" + Request.Files["file"].FileName));
return Content("ok");
}
在浏览器键入地址也可以访问到相关文件
https://localhost:44386/uploads/a.txt
② Response
响应一段文字:
public ActionResult Index()
{
Response.Write("hello,mvc");
return Content("finished");
}
展示请求头信息
public ActionResult Index()
{
// 展示请求头信息
return Content(Request.Headers["token"]);
}
// 设置响应头:
public ActionResult Index()
{
Response.Headers["userId"] = "linda123";
return Content("ok");
}
③Session
所有人各自的数据存储
本质上为一些键值对
持续时间为20min
当有互动时重新计算有效时间
存储的位置在服务器
但会影响服务器性能
常用于存储登录信息
所有的页面都可使用session数据
设置Session
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<form action="/Demo/Index" method="post">
<input type="type" name="user" value="" />
<input type="submit" name="name" value="提交" />
</form>
</body>
</html>
public ActionResult Index()
{
Session["user"] = Request.Form["user"];
//所有页面都可读取
return Content("会话数据是" + Session["user"]);
}
如何删除会话(安全退出)
public ActionResult Index()
{
Session["user"] = Request.Form["user"];
return Content("会话数据是" + Session["user"]);
}
正式学习MVC 01的更多相关文章
- 正式学习MVC 05
1.剃须刀模板razor的使用 1)混编 循环语法 @model List<MVCStudy.Models.Student> @{ ViewBag.Title = "List&q ...
- 正式学习MVC 02
1.cookie 继续讲解MVC的内置对象cookie 相对不安全 1)保存cookie public ActionResult Index() { // 设置cookie以及过期时间 Respons ...
- 正式学习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 01
ASP.NET MVC 01 - ASP.NET概述 本篇目录: ASP.NET 概述 .NET Framework 与 ASP.NET ASP.NET MVC简介 ASP.NET的特色和优势 典型案 ...
- 学习MVC之租房网站(二)-框架搭建及准备工作
在上一篇<学习MVC之租房网站(一)-项目概况>中,确定了UI+Service的“双层”架构,并据此建立了项目 接下来要编写Common类库.配置AdminWeb和FrontWeb 一.编 ...
- 软件测试之loadrunner学习笔记-01事务
loadrunner学习笔记-01事务<转载至网络> 事务又称为Transaction,事务是一个点为了衡量某个action的性能,需要在开始和结束位置插入一个范围,定义这样一个事务. 作 ...
随机推荐
- Ubuntu 设置静态 IP
一.背景 如果没有设置静态IP,由于某些情况,会导致系统的 IP 地址发生变化. 为了避免 IP 发生变化,就需要进行静态 IP 的设置. 注:这里 Ubuntu 版本为 19.10 二.解决方案 1 ...
- 2018 ACM-ICPC 宁夏 C.Caesar Cipher(模拟)
In cryptography, a Caesar cipher, also known as the shift cipher, is one of the most straightforward ...
- 一个简单的jQuery回调函数例子
jQuery回调函数简单使用 比如说,我们想要点击某个按钮后触发事件, 先把一些指定内容给隐藏掉, 然后跳出相关信息的对话框. 如果使用普通的方法, 不用回调函数的话, 会有怎么样的效果呢? 效果是先 ...
- 2. Unconstrained Optimization(2th)
2.1 Basic Results on the Existence of Optimizers 2.1. DefinitionLet $f:U->\mathbb{R}$ be a functi ...
- [LC] 520. Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not. We define the u ...
- 58)PHP,后台登录步骤示意图
注意上面的action提交位置: 并且注意下面的两个东西:
- Hypothesis Tests for One Population Mean When σ Is Unknown|other
9.5 Hypothesis Tests for One Population Mean When σ Is Unknown 使用t分布: What If the Assumptions Are No ...
- HDU-6707-Shuffle Card(很数据结构的一道题)
题目传送门 sol1:拿到这题的时候刚上完课,讲的是指针.所以一下子就联想到了双向链表.链表可以解决卡片移动的问题,但是无法快速定位要移动的卡片,所以再开一个指针数组,结合数组下标访问的特性快速定位到 ...
- 一个类似ThinkPHP的Node.js框架——QuickNode
QuickNode Node.js从QuickNode开始,让restful接口开发更简单! PHP的MVC 作为一名曾经的PHP开发者,我也有过三年多的thinkphp使用经验,那是我学习PHP接触 ...
- 吴裕雄--天生自然python编程:turtle模块绘图(3)
turtle(海龟)是Python重要的标准库之一,它能够进行基本的图形绘制.turtle图形绘制的概念诞生于1969年,成功应用于LOGO编程语言. turtle库绘制图形有一个基本框架:一个小海龟 ...