1. 目录结构

使用ASP.NET MVC模板创建的项目结构。
image.png

2. Layout

一般情况下,Controller下面的一个方法,对应Views某一个页面里的一个页面。

Views>Shared是共享的文件夹。里面的_Layout.chtml(称之为母版页)是一个完整的页面。里面有一个RenderBody()相当于一个占位符,用于替换Views里面的某一个页面。

_ViewStart.chtml设置了Layout

  1. @{
  2. Layout = "~/Views/Shared/_Layout.cshtml";
  3. }

不是特殊说明的情况下,所有的母版页都会自动套用_Layout.chtml
如果需要指定母版页,则在页面@{}中设置Layout即可。

  1. @{
  2. ViewBag.Title = "Index";
  3. Layout = "~";
  4. }
  5. <h2>我是Test</h2>

不需要母版页,把Layout设为null即可。

3. BundleConfig

打包器

  1. public class BundleConfig
  2. {
  3. // 有关捆绑的详细信息,请访问 https://go.microsoft.com/fwlink/?LinkId=301862
  4. public static void RegisterBundles(BundleCollection bundles)
  5. {
  6. bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
  7. "~/Scripts/jquery-{version}.js"));
  8. bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include(
  9. "~/Scripts/jquery.validate*"));
  10. // 使用要用于开发和学习的 Modernizr 的开发版本。然后,当你做好
  11. // 生产准备就绪,请使用 https://modernizr.com 上的生成工具仅选择所需的测试。
  12. bundles.Add(new ScriptBundle("~/bundles/modernizr").Include(
  13. "~/Scripts/modernizr-*"));
  14. bundles.Add(new ScriptBundle("~/bundles/bootstrap").Include(
  15. "~/Scripts/bootstrap.js"));
  16. bundles.Add(new StyleBundle("~/Content/css").Include(
  17. "~/Content/bootstrap.css",
  18. "~/Content/site.css"));
  19. }

比如下行代码:
bundles.Add(new ScriptBundle("~/bundles/jquery").Include(``"~/Scripts/jquery-{version}.js"));
意思是将~/Scripts/jquery-{version}.js"打包到虚拟目录~/bundles/jquery目录下面。
对于jquery库我们使用了这样的名称~/Scripts/jquery-{version}.js,其中{version}部分代表版本号的意思,MVC将会替我们在Scripts文件中寻找对应的"jquery-版本号.js"文件,并且在非debug模式下,MVC则会使用“jquery-版本号.min.js"文件。
我们还看到我们使用了这样的名称~/Scripts/jquery.validate*的名称,*是一个通配符,这就意味着Scripts文件夹下的所有前缀为jquery.validate的文件都将包含在同一个Bundle中。

接着在Global.asax文件的Application_Start方法中调用BundleConfig.RegisterBundles方法:

  1. public class MvcApplication : System.Web.HttpApplication
  2. {
  3. protected void Application_Start()
  4. {
  5. AreaRegistration.RegisterAllAreas();
  6. FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
  7. RouteConfig.RegisterRoutes(RouteTable.Routes);
  8. BundleConfig.RegisterBundles(BundleTable.Bundles);
  9. }
  10. }

最后,我们可以View上使用Bundle来代替原来引用的方式:

  1. @Styles.Render("~/Content/css")
  2. @Scripts.Render("~/bundles/jquery")

打包器作用,1:压缩文件 2:更新js文件的时候,满足打包规则不需要动调用js文件的地址

4. 内置对象

Request

  • 请求
  • 服务器接收客户端数据 ```csharp public class HomeController : Controller { // GET: Home public ActionResult Index() { // Content 是结果字符串 // QueryString 查询字符串 return Content(“response:” + Request.QueryString[“name”]); } }

// 请求url:http://localhost:63179/?name=123 // 响应内容:response:123

  1. 也可以使用下面方式
  2. ```csharp
  3. namespace WebMvc.Controllers
  4. {
  5. public class HomeController : Controller
  6. {
  7. // GET: Home
  8. public ActionResult Index(string name, string age)
  9. {
  10. // Content 是结果字符串
  11. return Content($"response: name:{name},age:{age}");
  12. }
  13. }
  14. }
  15. Url:http://localhost:63179/?name=123&age=18
  16. Response:response: name:123,age:18

post请求使用Request.Form接收。

  1. public ActionResult PostData()
  2. {
  3. // Request.Form post请求数据
  4. return Content("response:data:"+Request.Form["userName"]);
  5. }

上传文件使用Request.Files,需要注意的是保存位置,需要在项目中创建相对应的文件夹。上传完文件之后不会在文件夹中显示,因为上传的文件不数据该项目,未包含在目录内。

  1. <form action="/Home/PostFile" method="post" enctype="multipart/form-data">
  2. <input type="file" name="file" value="" />
  3. <button>上传</button>
  4. </form>
  1. public ActionResult PostFile()
  2. {
  3. // SaveAs方法需要物理路径
  4. // 使用Request.MapPath将虚拟路径映射到物理路径
  5. Request.Files["file"].SaveAs(Request.MapPath("~/Uploads/" +
  6. Request.Files["file"].FileName));
  7. return Content("Ok");
  8. }

Request.Headers请求头。

  1. public ActionResult RequestHeaders()
  2. {
  3. return Content(Request.Headers["token"]);
  4. }

使用Postman自定义Headers
image.png

Response

  • 响应
  • 服务器给客户端

Response.Write向客户端输出内容。

  1. public ActionResult ResponseData()
  2. {
  3. Response.Write("response-");
  4. return Content("Ok");
  5. }
  6. Url : http://localhost:63179/home/ResponseData
  7. Response : response-Ok

Response.Redirect("https://www.baidu.com"); 重定向至百度。
Response.StatusCode状态码。

  1. public ActionResult ResponseData()
  2. {
  3. //Response.Write("response-");
  4. Response.Redirect("https://www.baidu.com");
  5. return Content("Ok");
  6. }

Response.Headers响应头。

  1. public ActionResult ResponseHeaders()
  2. {
  3. return Content(Response.Headers["hello"] = "hello");
  4. }

image.png

Session

  • 会话。
  • 从浏览器进入网站开始就启动了一个session,每个人的session是独立的。待机时效20min。每个人独立。安全。
  • Session保存在服务器中。
  • 存储少量重要数据,比如账号。

Session

  1. public ActionResult SessionData()
  2. {
  3. Session["user"] = Request.Form["user"];
  4. return Content("session:" + Session["user"]);
  5. }
  6. public ActionResult TestSession()
  7. {
  8. return Content("当前user:" + Session["user"]);
  9. }

使用Html跳转
image.png
image.png
测试Session
image.png
清除Session

  1. public ActionResult ClearSession()
  2. {
  3. // 销毁
  4. Session.Abandon();
  5. // 或者清除
  6. Session.Clear();
  7. return Content("当前user:" + Session["user"]);
  8. }

Cookie

  • 客户端数据
  • 不安全。

创建一个CookieResponse.Cookies

  1. public ActionResult CookieSave()
  2. {
  3. Response.Cookies.Add(new HttpCookie("token")
  4. {
  5. // 内容
  6. Value = "ResponseCookie",
  7. // 过期时间
  8. Expires = DateTime.Now.AddDays(7)
  9. });
  10. return Content("Ok");
  11. }

image.png
Request.Cookies获取Cookie
注意获取的时候,如果没有对应的Cookie,获取的时候是会报错,需判断。

  1. public ActionResult GetCookie()
  2. {
  3. return Content("Cookie:" + Request.Cookies["token"].Value);
  4. }
  5. Url http://localhost:63179/home/GetCookie
  6. Response : Cookie:ResponseCookie

清除Cookie

  1. public ActionResult ClearCookie()
  2. {
  3. Response.Cookies.Add(new HttpCookie("token")
  4. {
  5. Expires = DateTime.Now.AddDays(-1)
  6. });
  7. return Content("Ok");
  8. }

Application

  • 当前网站对象
  • Session差不多的使用方式。区别在于Application是全局使用的。 ```csharp public ActionResult ApplicationData() { HttpContext.Application[“user”] = “applicationAdmin”; return Content(“Ok”); }

public ActionResult ApplicationGet() { // 注意空对象 return Content(HttpContext.Application[“user”].ToString()); } ```

Server

  • 服务器对象
  • 包含了服务器一些常用的方法。

Server.Transfer()方法,路径不变,内容改变。不能转发外站的内容。