正式学习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的性能,需要在开始和结束位置插入一个范围,定义这样一个事务. 作 ...
随机推荐
- Android recycleview item水波纹效果
item的xml 根标签下添加如下三个属性 android:clickable="true" android:focusable="true" android: ...
- python中的if not
在python中 None, False, 空字符串"", 0, 空列表[], 空字典{}, 空元组()都相当于False ,即: not None == not False = ...
- ui-choose|列表选择jQuery美化插件
ui-choose是一款基于jQuery的列表选择美化插件.ui-choose可用于选项不太多的select.radio.checkbox等,提升用户体验. 使用方法 使用ui-choose列表美化插 ...
- RELAX NG
RELAX NG (读作"relaxing"), 是一种基于语法的XML模式语言,可用于描述.定义和限制XML词汇表. 最初的XML模式语言是DTD,但是因为DTD语法丑陋, 表达 ...
- KMP算法 详解+模板
本文大部分摘自szy学长的ppt<string>中的KMP部分. %%%膜拜szy大神orz 1.概述 KMP 算法是用来解决单模匹配问题的一种算法. 如果暴力的进行单模匹配,那么时间复杂 ...
- [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 ...
- jenkins使用(2)-配置项目代码的3种方式
1.通过cmd命令直接进入项目代码的文件夹运行,注意路径中不要有中文 2.代码放到工作区:从本地复制项目代码到工作区目录下 代码结构的优化 3.代码连接git或svn,实时更新代码 svn检出 然后上 ...
- python实现经典冒泡算法
利用for循环,完成a=[1,7,4,89,34,2]的冒泡排序 冒泡排序:小的排在前,大的排在后面
- fetch API 和 ajax
fetch('/some.json', { method: 'get', body: { id: 22 } }).then(function (resp) { resp.json().then(con ...
- LVS+Keepalived 配置
LVS+Keepalived配置 环境准备 LVS1:192.168.1.1 LVS2:192.168.1.2 MySQL Server1:192.168.1.13 MySQL Server2:192 ...