深度解析 ASP.NET MVC 5
分享公司內部培訓資料,若有錯誤,請批評指正。html
- ASP.NET MVC基礎
- IoC容器
- ASP.NET MVC可擴展性
- ASP.NET MVC Filters & Cache
- ASP.NET MVC AJAX
- ASP.NET MVC Client Validation
- 資源文件目錄結構
- Model目錄結構
- View目錄結構
- View分解原則
- 關於Web.config
1. ASP.NET MVC基礎
1.1. 一次請求的生命週期
![](http://static.javashuo.com/static/loading.gif)
- 一次請求從客戶端/瀏覽器開始,客戶端將請求發送給IIS,IIS啓動ASP.NET MVC對應的應用程序。
- 在ASP.NET MVC內部,經過路由(Routing)開始匹配的路由規則。
- 在執行Action以前,首先會執行 Action 對應的 Filter,如 [Authorize]
- 而後執行 Action,這裏即是執行咱們的業務邏輯。
- 若是有視圖/頁面,View Engine返回相應的HTML。
- 在Action執行完成後,兩樣能夠執行Filter來處理相應邏輯。
1.2. Application 啓動
![](http://static.javashuo.com/static/loading.gif)
- MvcApplication繼承自 HttpApplication 類,ASP.NET MVC啓動後,首先執行的就是這個類。
- 最早進入的是 Application_Start 方法,在這個方法裏,能夠初始化基本的應用/類庫。
- 另外一種初始化方式如上圖中的下方的代碼截圖,以特性的方式初始化應用/類庫。
1.3. Routing 兩種形式
![](http://static.javashuo.com/static/loading.gif)
在ASP.NET MVC 5中,定義Route有兩種形式:瀏覽器
- 在 Application_Start 中定義 Route, 如上圖中上方截圖。
- 在 Controller/Action 中以 Attribute 方式直接定義 Route,如上圖中下方截圖。
1.4. Filters (ActionFilterAttribute)
![](http://static.javashuo.com/static/loading.gif)
- 前面咱們提到過,在執行Action以前和以後,能夠執行一些Filter處理一些事情,如 [Authorize],就是在執行 Action 以前,判斷必須有帳號登陸。
- 咱們也能夠自定義一些Filter來處理咱們本身的事情,如角色權限判斷,上圖中左側截圖便是一個自定義的 Filter。
1.5. Controller & Action
![](http://static.javashuo.com/static/loading.gif)
- Controller是全部Controller的基類,全部Controller必須繼承自Controller類。
- Controller類能夠使用 Filter,表示此類下的全部Action都應用這個 Fitler。
- Action能夠應用 GET, POST, PUT 和 DELETE 等方法。
- Action返回的結果能夠是 View, PartialView, Redirect, Json, JavaScript, Content 和 File 等類型。
1.6. View / View Engine
![](http://static.javashuo.com/static/loading.gif)
- View Engine能夠被重寫和定義,這裏使用的 Razor View Engine。
- 使用 View() 和 PartialView() 返回視圖/HTML。
- 查找視圖時,若是未指定路徑,默認查找無則是 Views/{controller name}/{action name}.cshtml,若是沒有則查找 Views/Shared/{action name}.cshtml。
- Shared/_Layout.cshtml是View的默認框架模板,也能夠View中單獨指定。
2. IoC容器
2.1. 什麼是IoC容器?
![](http://static.javashuo.com/static/loading.gif)
2.2. Unity
![](http://static.javashuo.com/static/loading.gif)
3. ASP.NET MVC可擴展性
3.1. Controller 擴展性
![](http://static.javashuo.com/static/loading.gif)
3.2. Model 擴展性
![](http://static.javashuo.com/static/loading.gif)
3.3. View Engine
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
4. ASP.NET MVC Filters & Cache
![](http://static.javashuo.com/static/loading.gif)
5. ASP.NET MVC AJAX
![](http://static.javashuo.com/static/loading.gif)
6. ASP.NET MVC Client Validation
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
7. 資源文件目錄結構
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
8. Model目錄結構
![](http://static.javashuo.com/static/loading.gif)
9. View目錄結構
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
10. View分解原則
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
11. 關於Web.config
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)