初识 ASP.NET 3.5 MVC 开发
在學習被停滯了N久以后,今天終于下定決心要繼續了。過了太久墮落的生活也開始厭倦了。繼續開始我的MVC學習之路。
目錄機構:
models 文件夾: 模型組件??? 還可以存放有關數據訪問操作的一些類、對象的操作的定義等。
Views 文件夾: 視圖組件。可以存放的文件類型包括.aspx頁面,.ascx控件以及.master母版頁等。
Shared文件夾:視圖組件中的公用部分。可以存放 母版頁、CSS樣式等文件。
Controllers文件夾:控制器組件。
?
在Web.Config中注冊了 UrlRoutingModule類,用于解析URL路由。
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />在Global.asax文件中的Application_Start()方法中設置了URL路由,以及相關的路由邏輯。
public static void RegisterRoutes(RouteCollection routes){routes.IgnoreRoute("{resource}.axd/{*pathInfo}");routes.MapRoute("Default", // Route name"{controller}/{action}/{id}", // URL with parametersnew { controller = "Home", action = "Index", id = "" } // Parameter defaults);}protected void Application_Start(){RegisterRoutes(RouteTable.Routes);}}?
?
執行過程:
當執行基于ASP.NET 3.5 MVC 框架的MVCApplication網站時,根據瀏覽器中的URL地址,該URL地址首先被傳遞到URLRoutingModule 模塊,該模塊解析該URL地址,然后選擇相關的URL路由,并得到兌現的IHttpContext對象來處理該URL路由。在默認情況下,該IHttpContext對象就是MvcHandler 對象。通過MvcHandler對象,選擇相關的控制器來處理用戶的請求。
執行步驟:
在基于ASP.NET 3.5 MVC 框架的MVCApplication網站中,每一個請求的頁面都被映射到相應的控制器中的相關方法,控制器負責將制定的內容返回到瀏覽器中。多個頁面可以被映射到同一個控制器中的不同方法。
在ASP.NET 3.5 MVC框架中,頁面到控制器的映射是通過路徑表(Route Table)而實現的,對于每一個應用程序有一個路徑表。路徑表通過RouteTable.Routes 屬性表示。
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");routes.MapRoute("Default", // Route name"{controller}/{action}/{id}", // URL with parametersnew { controller = "Home", action = "Index", id = "" } // Parameter defaults);當請求一個基于ASP.NET 3.5 MVC 框架的網站頁面時,在Web.config配置文件中所配置是UrlRoutingModule模塊解析該URL,并獲得相關的RouteData對象,然后創建HttpHandler的實例化對象MvcHandler。
在執行MvcHandler時,調用幾種的ProcessRequest()方法,執行該ProcessRequest()方法,從而創建一個控制器的實例化對象。
在執行Controller時,調用其中的Execute()方法,在該方法內部通過反射原理實現對指定其他方法的調用,在調用的方法中會執行View()方法,從而將指定頁面的內容返回到瀏覽器中~
?
一個增刪改查的例子:
HomeController:
[HandleError]public class HomeController : Controller{NorthwindEntities northwind = new NorthwindEntities();public ActionResult Index(){var model = northwind.Categories.ToList();return View(model);}public ActionResult About(){return View();}[AcceptVerbs(HttpVerbs.Get)]public ActionResult Edit(int id){var model = northwind.Categories.First(c => c.CategoryID == id);return View(model);}[AcceptVerbs(HttpVerbs.Post)]public ActionResult Edit(int id,FormCollection from){var model = northwind.Categories.First(c => c.CategoryID == id);UpdateModel(model,new [] {"CategoryName","Description"});northwind.SaveChanges();return RedirectToAction("index");}[AcceptVerbs(HttpVerbs.Get)]public ActionResult Detail(int id){var model = northwind.Categories.First(c => c.CategoryID == id);return View(model);}[AcceptVerbs(HttpVerbs.Get)]public ActionResult Create(){Categories categories = new Categories();return View(categories);}public ActionResult Create(int CategoryID, FormCollection form){var model = northwind.Categories.FirstOrDefault(c => c.CategoryID == CategoryID);if (model==null){Categories categories = new Categories();UpdateModel(categories, new[] {"CategoryName", "Description"});northwind.AddToCategories(categories);northwind.SaveChanges();return RedirectToAction("Index");}else{return RedirectToAction("Create");}}}?
感覺剛剛看了點皮毛還是很簡單的,不過以后在實用上面估計還是要多多下功夫的。加油吧。繼續學習中……………………………………………………
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的初识 ASP.NET 3.5 MVC 开发的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 揭秘ASP.NET 2.0的Eval方法
- 下一篇: Call 从一个批处理程序调用另一个批处