在上一篇的文章中,我們大概理解MVC 的模式后,我們應(yīng)該是從基礎(chǔ)入手:
創(chuàng)新互聯(lián)公司專注于興海網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供興海營(yíng)銷型網(wǎng)站建設(shè),興海網(wǎng)站制作、興海網(wǎng)頁(yè)設(shè)計(jì)、興海網(wǎng)站官網(wǎng)定制、微信平臺(tái)小程序開發(fā)服務(wù),打造興海網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供興海網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
1.我們先理解一下程序的入口
使用了URL重寫. ASP.NET中叫做UrlRouting,對(duì)應(yīng)的程序集是System.Web.Routing, 打開項(xiàng)目的Global.asax.cs文件, 會(huì)找到我們建立的頁(yè)面重寫規(guī)則
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Users", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); } protected void Application_Start() { AreaRegistration.RegisterAllAreas(); System.Data.Entity.Database.SetInitializer(new MvcApplication1.Models.UsersInitializer()); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); }
2.查找 Controller
在Controllers文件夾下我們可以找到UsersController.cs, 這里使用了一個(gè)約定, 就是如果URL中獲取到的Controller名字是Users, 則他的Controller類名就是UsersController. 在URL中的名字后加上”Controller”.
實(shí)例中Controller都放在Controllers文件夾, 所以我們可以按照命名約定很容易就可以找到HomeController類
3.查找 Action
一個(gè)Controller可以包含多個(gè)Action, MVC模式中Controller角色的具體實(shí)現(xiàn)邏輯都是在Action中的.
因?yàn)槲覀兊腁ction是Index, 所以自然就要調(diào)用Index()方法.這里將"Webcome to ASP.NET MVC!",
private UsersDbContext db = new UsersDbContext(); // // GET: /Users/ public ViewResult Index() { return View(db.Users.ToList()); } // // GET: /Users/Details/5 public ViewResult Details(int id) { Users users = db.Users.Find(id); return View(users); } // // GET: /Users/Create public ActionResult Create() { return View(); } // // POST: /Users/Create [HttpPost] public ActionResult Create(Users users) { if (ModelState.IsValid) { db.Users.Add(users); db.SaveChanges(); return RedirectToAction("Index"); } return View(users); } // // GET: /Users/Edit/5 public ActionResult Edit(int id) { Users users = db.Users.Find(id); return View(users); } // // POST: /Users/Edit/5 [HttpPost] public ActionResult Edit(Users users) { if (ModelState.IsValid) { db.Entry(users).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(users); } // // GET: /Users/Delete/5 public ActionResult Delete(int id) { Users users = db.Users.Find(id); return View(users); } // // POST: /Users/Delete/5 [HttpPost, ActionName("Delete")] public ActionResult DeleteConfirmed(int id) { Users users = db.Users.Find(id); db.Users.Remove(users); db.SaveChanges(); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) { db.Dispose(); base.Dispose(disposing); }
4.查找view 視圖
View方法中可以帶一個(gè)名字, 這個(gè)名字就是View的名字.如果把index改成Edit,那么訪問/Users/Edit就會(huì)跳轉(zhuǎn)到Edit頁(yè)!
5. 頁(yè)面展示
接下來ViewEngine即頁(yè)面引擎會(huì)將aspx中的HTML部分以及上面的數(shù)據(jù)部分和在一起返回給瀏覽器.
關(guān)于View對(duì)象我注意到此頁(yè)面是繼承自System.Web.Mvc.ViewPage而不是直接繼承自System.Web.UI.Page, 而這個(gè)ViewData對(duì)象就是ViewPage中的一個(gè)屬性. 這里的ViewData一定是頁(yè)面級(jí)別的,當(dāng)頁(yè)面編譯完畢這個(gè)對(duì)象就會(huì)被注銷(HTTP是無狀態(tài)的協(xié)議,每次請(qǐng)求其實(shí)都是生成一個(gè)新的ViewPage對(duì)象).
@model IEnumerable<MvcApplication1.Models.Users> @{ ViewBag.Title = "Index"; } <h3>Index</h3> <p> @Html.ActionLink("Create New", "Create") </p> <table> <tr> <th> UserID </th> <th> UserName </th> <th> Password </th> <th> RegTime </th> <th> IsTest </th> <th> IsEnable </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.UserID) </td> <td> @Html.DisplayFor(modelItem => item.UserName) </td> <td> @Html.DisplayFor(modelItem => item.Password) </td> <td> @Html.DisplayFor(modelItem => item.RegTime) </td> <td> @Html.DisplayFor(modelItem => item.IsTest) </td> <td> @Html.DisplayFor(modelItem => item.IsEnable) </td> <td> @Html.ActionLink("Edit", "Edit", new { id=item.ID }) | @Html.ActionLink("Details", "Details", new { id=item.ID }) | @Html.ActionLink("Delete", "Delete", new { id=item.ID }) </td> </tr> } </table> 效果圖:
文章題目:學(xué)習(xí)Asp.netMVC-牛刀小試(1)
標(biāo)題URL:http://m.kartarina.com/article22/pihejc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、Google、云服務(wù)器、網(wǎng)站維護(hù)、全網(wǎng)營(yíng)銷推廣、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)