简介
本教程基于微软ASP.NET-MVC5官方文档修订而成。文中将VS版本从官方文档的VS2013修改为2015,并且增加了基础内容,缩减了高级部分,形成简明版本,以便入门。
MVC框架的演变
- MVC1-MV4的演变可以参考如下:
- MVC1-MVC2
- MVC3-MVC4
- MVC5是针对之前版本的一个集大成者,几乎完美,只是不推荐跨平台。
- MVC6是一个颠覆版本,构建于ASP.NET5之上,为跨平台而生。
Pre-Requirement
- Visual Studio 2015
- ASP.NET 4.6.1
- IIS 7.5+
Source Code
Quick Start
Create a New MVC Project

newProject.png

newProjectWeb.png

newProjectMVC.png
Setting IISExpress

iisExpressShowAll.png

iisExpressSetting.png
- 按F5会开始Debug,VS会直接以Attach的方式启动IISExpress
- 按Ctrl + F5,不debug,此时可以修改源代码,编译后刷新Browser页面可以应用新代码
- Ctrl + Alt + P: 可以Attach到指定的进程
Router
Add Router

routerGlobal.png

routerFormat.png
- 按F12,可以进入定义位置
- 上图中,url表示了Route的样式:
- 默认的Controller是Home
- 对所有的Controller,默认的Action都是Index
- id是可选的
Controller
Add Controller

addController.png

addControllerMVC5.png

addControllerName.png
QureyString
- QueryString字符串会自动传给Action方法对应的形参
- 我们可以在Action方法中指定默认参数

QueryString.png

QueryStringDefault.png
- 这里的HttpUtility.HtmlEncode 也可以写成 Server.HtmlEncode,Server是HttpUtility的一个实现
Validate Request
- 如果我们输入可能造成注入攻击的内容,可以看到页面报错,这个Request Validation机制和HtmlEncode无关,是在MiddleWare层做掉的。

RequestValidationError.png
- 如果要屏蔽此报错,可以参考MSDN,请注意:
- 针对WebForm / MVC / Web Pages有不同的改法
- 该步骤适用于.NET 4.5及以上版本
Encode HTML
- 如果不使用HTMLEncode,虽然RequestValidation可以通过,但我们依然无法把HtmlTag等字符注入到页面中。

HTMLEncode1.png

HTMLEncode2.png

HTMLEncode3.png
View
Add View

AddView1.png

AddView2.png
Template-CSHTML
- Control通过View()方法关联到CSHTML

cshtml1.png

cshtml2.png

cshtml3.png

ViewLayout1.png

ViewLayout2.png
Controller & View

cshtml4.png
- 如何将Controller中的变量传递给View?

cshtml5.png
Model
Add Movie Model

AddModel1.png

AddModel2.png

AddModel3.png

AddModel5.png

AddModel4.png

AddModel6.png
Add Movie Controller

AddMovieController1.png

AddMovieController2.png

AddMovieController3.png
- 现在我们就可以通过WebPage操作Movie模型了

ShowMovieEditPage.png
LocalDB

ShowAppData1.png

ShowAppData2.png
- 在Server Explorer里可以打开MovieDBContext

VSDBContext1.png

VSDBContext2.png

VSDBContext3.png

VSDBContext4.png

VSDBContext5.png

VSDBContext6.png

VSDBContext7.png

VSDBContext8.png
Publish
User Profile
-
SQL Express需要Load一个user profile,但IIS7.5默认是不会Load User Profile的。所以部署到IIS(IIS7.5,2008R2)后,LocalDB不能被直接支持,会报错:
A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 50 – Local Database Runtime error occurred. Cannot create an automatic instance. See the Windows Application event log for error details.
)
-
可以参考:
- IIS论坛
- MSDN Blog
-
需要在ApplicationHost.config中添加支持
<system.applicationHost>
<applicationPools>
<add name="testMVC" managedRuntimeVersion="v4.0">
<processModel loadUserProfile="true" setProfileEnvironment="true"/>
</add>
</applicationPools>
</system.applicationHost>
LocalDB Write Permisson
- IIS-AppPool账号对LocalDB文件需要有写权限

SetPermission1.png

SetPermission2.png