初識 ASP.NET 3.5 MVC 開發

來源:互聯網
上載者:User

在學習被停滯了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 parameters                new { 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 parameters                new { 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");            }        }    }

 

感覺剛剛看了點皮毛還是很簡單的,不過以後在實用上面估計還是要多多下功夫的。加油吧。繼續學習中……………………………………………………

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.