1. 目录结构
使用ASP.NET MVC模板创建的项目结构。
2. Layout
一般情况下,Controller下面的一个方法,对应Views某一个页面里的一个页面。
Views>Shared是共享的文件夹。里面的_Layout.chtml(称之为母版页)是一个完整的页面。里面有一个RenderBody()相当于一个占位符,用于替换Views里面的某一个页面。
_ViewStart.chtml设置了Layout
@{Layout = "~/Views/Shared/_Layout.cshtml";}
不是特殊说明的情况下,所有的母版页都会自动套用_Layout.chtml。
如果需要指定母版页,则在页面@{}中设置Layout即可。
@{ViewBag.Title = "Index";Layout = "~";}<h2>我是Test</h2>
不需要母版页,把Layout设为null即可。
3. BundleConfig
打包器
public class BundleConfig{// 有关捆绑的详细信息,请访问 https://go.microsoft.com/fwlink/?LinkId=301862public static void RegisterBundles(BundleCollection bundles){bundles.Add(new ScriptBundle("~/bundles/jquery").Include("~/Scripts/jquery-{version}.js"));bundles.Add(new ScriptBundle("~/bundles/jqueryval").Include("~/Scripts/jquery.validate*"));// 使用要用于开发和学习的 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"));}
比如下行代码: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方法:
public class MvcApplication : System.Web.HttpApplication{protected void Application_Start(){AreaRegistration.RegisterAllAreas();FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);RouteConfig.RegisterRoutes(RouteTable.Routes);BundleConfig.RegisterBundles(BundleTable.Bundles);}}
最后,我们可以View上使用Bundle来代替原来引用的方式:
@Styles.Render("~/Content/css")@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
也可以使用下面方式```csharpnamespace WebMvc.Controllers{public class HomeController : Controller{// GET: Homepublic ActionResult Index(string name, string age){// Content 是结果字符串return Content($"response: name:{name},age:{age}");}}}Url:http://localhost:63179/?name=123&age=18Response:response: name:123,age:18
post请求使用Request.Form接收。
public ActionResult PostData(){// Request.Form post请求数据return Content("response:data:"+Request.Form["userName"]);}
上传文件使用Request.Files,需要注意的是保存位置,需要在项目中创建相对应的文件夹。上传完文件之后不会在文件夹中显示,因为上传的文件不数据该项目,未包含在目录内。
<form action="/Home/PostFile" method="post" enctype="multipart/form-data"><input type="file" name="file" value="" /><button>上传</button></form>
public ActionResult PostFile(){// SaveAs方法需要物理路径// 使用Request.MapPath将虚拟路径映射到物理路径Request.Files["file"].SaveAs(Request.MapPath("~/Uploads/" +Request.Files["file"].FileName));return Content("Ok");}
Request.Headers请求头。
public ActionResult RequestHeaders(){return Content(Request.Headers["token"]);}
Response
- 响应
- 服务器给客户端
Response.Write向客户端输出内容。
public ActionResult ResponseData(){Response.Write("response-");return Content("Ok");}Url : http://localhost:63179/home/ResponseDataResponse : response-Ok
Response.Redirect("https://www.baidu.com"); 重定向至百度。Response.StatusCode状态码。
public ActionResult ResponseData(){//Response.Write("response-");Response.Redirect("https://www.baidu.com");return Content("Ok");}
Response.Headers响应头。
public ActionResult ResponseHeaders(){return Content(Response.Headers["hello"] = "hello");}
Session
- 会话。
- 从浏览器进入网站开始就启动了一个
session,每个人的session是独立的。待机时效20min。每个人独立。安全。 - Session保存在服务器中。
- 存储少量重要数据,比如账号。
Session
public ActionResult SessionData(){Session["user"] = Request.Form["user"];return Content("session:" + Session["user"]);}public ActionResult TestSession(){return Content("当前user:" + Session["user"]);}
使用Html跳转

测试Session
清除Session
public ActionResult ClearSession(){// 销毁Session.Abandon();// 或者清除Session.Clear();return Content("当前user:" + Session["user"]);}
Cookie
- 客户端数据
- 不安全。
创建一个CookieResponse.Cookies
public ActionResult CookieSave(){Response.Cookies.Add(new HttpCookie("token"){// 内容Value = "ResponseCookie",// 过期时间Expires = DateTime.Now.AddDays(7)});return Content("Ok");}

Request.Cookies获取Cookie
注意获取的时候,如果没有对应的Cookie,获取的时候是会报错,需判断。
public ActionResult GetCookie(){return Content("Cookie:" + Request.Cookies["token"].Value);}Url : http://localhost:63179/home/GetCookieResponse : Cookie:ResponseCookie
清除Cookie
public ActionResult ClearCookie(){Response.Cookies.Add(new HttpCookie("token"){Expires = DateTime.Now.AddDays(-1)});return Content("Ok");}
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()方法,路径不变,内容改变。不能转发外站的内容。
