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的更多相关文章

  1. 正式学习MVC 05

    1.剃须刀模板razor的使用 1)混编 循环语法 @model List<MVCStudy.Models.Student> @{ ViewBag.Title = "List&q ...

  2. 正式学习MVC 02

    1.cookie 继续讲解MVC的内置对象cookie 相对不安全 1)保存cookie public ActionResult Index() { // 设置cookie以及过期时间 Respons ...

  3. 正式学习MVC 06

    1.Model常用属性讲解 using System; using System.ComponentModel.DataAnnotations; namespace MVCStudy2.Models ...

  4. 正式学习MVC 04

    1.ActionResult ActionResult是一个父类, 子类包括了我们熟知的 ViewResult 返回相应的视图 ContentResult  返回字符串 RedirectResult( ...

  5. 正式学习MVC 03

    1.View -> Controller的数据通信 1) 通过url查询字符串 public ActionResult Index(string user) { return Content(u ...

  6. 白话学习MVC(十)View的呈现二

    本节将接着<白话学习MVC(九)View的呈现一>来继续对ViewResult的详细执行过程进行分析! 9.ViewResult ViewResult将视图页的内容响应给客户端! 由于Vi ...

  7. MVC 01

    ASP.NET MVC 01 - ASP.NET概述 本篇目录: ASP.NET 概述 .NET Framework 与 ASP.NET ASP.NET MVC简介 ASP.NET的特色和优势 典型案 ...

  8. 学习MVC之租房网站(二)-框架搭建及准备工作

    在上一篇<学习MVC之租房网站(一)-项目概况>中,确定了UI+Service的“双层”架构,并据此建立了项目 接下来要编写Common类库.配置AdminWeb和FrontWeb 一.编 ...

  9. 软件测试之loadrunner学习笔记-01事务

    loadrunner学习笔记-01事务<转载至网络> 事务又称为Transaction,事务是一个点为了衡量某个action的性能,需要在开始和结束位置插入一个范围,定义这样一个事务. 作 ...

随机推荐

  1. 筛选nginx访问日志文件中的域名

    head  -n 500 1.log |awk  '{print $11}' > 1.txt     查看1.log日志文件前500行记录并打印出第11列也就是域名的那一列,并输出到1.txt文 ...

  2. D. Array Splitting(后缀数组)

    You are given an array

  3. G - KiKi's K-Number(树状数组求区间第k大)

    For the k-th number, we all should be very familiar with it. Of course,to kiki it is also simple. No ...

  4. 服务端向客户端推送消息技术之websocket的介绍

    websocket的介绍 在讲解WebSocket前,我们先来看看下面这种场景,在HTTP协议下,怎么实现. 需求: 在网站中,要实现简单的聊天,这种情况怎么实现呢?如下图: 当发送私信的时候,如果要 ...

  5. Linux的基础知识

    什么是操作系统? 操作系统是人与计算机的中介. 操作系统是干什么的? 控制所有资源{硬件资源和软件资源(驱动,应用软件)} 常用的操作系统:Unix Windows Linux Linux的哲学思想: ...

  6. 解决2013Lost connection to MySQL server during query错误方法

    在my.ini配置文件 mysqld 节点下添加 max_allowed_packet = 500M 也就是配置MySQL允许的最大数据包大小,上面的500M你可以根据你的项目修改为你自己的值,只要比 ...

  7. 网站爬取-案例四:知乎抓取(COOKIE登录抓取个人中心)(第二卷)

    接着上卷来分析,作为开发人员我们都知道,登录是一个想指定URL发送POST请求的过程,所以我们需要找到请求的URL,以及字段,先用一个错误账号和密码做一下尝试,如果是正确的话会直接跳转到别的页面,这样 ...

  8. 因为AI,所以爱

    作为技术驱动型公司 自我颠覆的核心就是技术上有所突破 2019技术奇点大会上,创始人行在提出 「未来,大数据和人工智能 将成为商业升级的智能发动机」 这与我们的使命不谋而合 时间退回到2016年的冬天 ...

  9. Unable to preventDefault inside passive event listener due to target being treated as passive. See https://www.chromestatus.com/features/5093566007214080

    解决办法: 两个方案:1.注册处理函数时,用如下方式,明确声明为不是被动的window.addEventListener('touchmove', func, { passive: false }) ...

  10. NI Vision 介绍

    NI Vision主要包括三种主要软件包: 主程序包(Vision Acquisition Software), 视觉开发模块(Vision Development Module), 以及用于自动检测 ...