Bài s 3
Tng quan v ASP.NET MVC Framework
Table of Contents
1 Tại sao sử dụng ASP.NET MVC 2
1.1 ASP.NET MVC là gì 2
1.1.1 Mô hình MVC cơ bản 2
1.1.2 Một vài đặc tính trong ASP.NET MVC 2
1.2 Sự khác biệt với WebForm 3
2 Ví dụ xây dựng ứng dụng với ASP.NET MVC Framework 4
2.1 Tạo Project với ASP.NET MVC Web Application 4
2.2 Tìm hiểu định tuyến URL 8
2.3 Xây dựng mô hình dữ liệu 9
2.3.1 Tao cơ s dữ liệu 9
2.3.2 S dung mô hình LINQ to SQL 10
2.4 Tìm hiểu về Controllers 14
2.5 Tìm hiểu về Views 16
2.6 Tìm hiểu về Models 18
2.7 Cấu trúc Views\Shared\Site.Master 20
2.8 Kết quả 21
3 Câu hỏi ôn tập 21
4 Tài liệu tham khảo 22
Microsoft Vietnam – DPE Team |Bài số 3: Tổng quan về ASP.NET MVC Framework
2 1 Tại sao s dụng ASP.NET MVC
1.2 Sự khác biệt với WebForm
ASP.NET WebForm s d quu có lifecycle, postback và dùng các
thc hing cho UI khi có s tác vi dùng nên hu ht
ASP.NET WebForm x lý chm.
ASP.NET MVC Framework chia ra thành 3 phn: Models, Views, Controllers. Mi dùng vi
Views s c thc hi ng trong Controllers, không còn postback, không còn lifecycle không còn
events.
Vic kim tra ( test ), g li ( debug ) vu phi chy tt c các tin trình ca ASP.NET và mi s
i ID ca bt k n ng di vi ASP.NET MVC Framework thì vic
có th s dng các unit test có th thnh rt d dàng các Controller thc hi nào.
Tính năng
ASP.NET 2.0
ASP.NET MVC
Ki
Kin trúc mô hình WebForm
Business Database
Kin trúc s dng vic phân chia
Models, Views
S dng cú pháp ca webform, tt
các s kin và controls do server
qun lý
Các s ki u khin bi
controllers, các controls không do
server do server qun lý
Truy cp d liu
S dng hu ht các công ngh truy
cp d liu trong ng dng
Phn ln dùng LINQ to SQL class
4
2 Ví dụ xây dựng ứng dụng với ASP.NET MVC Framework
n gi mi là b xây dng ng
dng vi ASP.NET MVC Framework cng hong .NET Framework 3.5 ( c th
sau s xây dng vi Visual Studio 2008 ), tip theo cn mt MVC Framework. Mnh trong Visual Studio
n ASP.NET MVC Web Applications. Có th download ASP.NET MVC Framework ta ch
/>2362BDDE0766&displaylang=ent ASP.NET MVC Framework. Bây
gi có th bu xây dng ng dng vi MVC.
2.1 To Project
.NET MVC Web Application
t ASP.NET MVC Framework: File New Project Windows
C# ASP.NET MVC Web Application ( xem Figure 2).
Figure 2.
Khi to mt ASP.NET MVC Framework thì mt hp thoi Unit Test xut hin. Chn Yes nu mun to mt
Project Test, chn No nu không mun to mt Unit Test. ( xem Figure 3)
Microsoft Vietnam – DPE Team |Bài số 3: Tổng quan về ASP.NET MVC Framework
5 Figure 3.
Sau khi mt ng dc to, nhìn vào trong mc Solution Explorer s thy
xut hin mc: Models, Views, Controllers chng vi các thành phn
Models, Views, Controllers trong mô hình MVC.
M rng folder Controllers s thy HomeController.cs, m rng Views s thy folder Home, Shared và Account.
M rng folder Home s thy About.aspx và Index.aspx ( xem hình Figure 4 )
Microsoft Vietnam – DPE Team |Bài số 3: Tổng quan về ASP.NET MVC Framework
6
namespace HiTest
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit
public class MvcApplication : System.Web.HttpApplication
{
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);
}
}
}
Khi ng dng chy lu tiên, phc gc này gi m
th to ra bnh tuyn.
New Item
.mdf
Figure 7. .mdf trong App_Data
.
2 table: ( xem Figure 8, Figure 9 )
Microsoft Vietnam – DPE Team |Bài số 3: Tổng quan về ASP.NET MVC Framework
10 Figure 8.
.mdf
2
Trong file DataClasses.
dd Association
2 table. (Figure 11)
Microsoft Vietnam – DPE Team |Bài số 3: Tổng quan về ASP.NET MVC Framework
13 Figure 11. .dbml
12
.
Figure 12.
return View();
}
public ActionResult About()
{
ViewData["Title"] = "About Page";
return View();
}
}
}
Trong HomeController.cs c là Index() và About() c này là 2 action trong
controller HomeController.cs nó thc hi c gi b a ch /Home/Index và /Home/About. Bt k
c nào có thuc tính public u là mt action trong controller.
T
Trong folder Controllers New Item MVC Controller Class ( Figure 13)
Microsoft Vietnam – DPE Team |Bài số 3: Tổng quan về ASP.NET MVC Framework
15 Figure 13. .cs
SanPhamController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using BanHang.Models;
List<SanPham> sp = data.LaySanPhamTuLoaiSanPham(loaisanpham);
return View("DanhSachSanPham", sp);
} public ActionResult ChiTietSanPham(int id)
{
ViewData["Title"] = "Chi tiết sản phẩm";
SanPham ctsp = data.LaySanPhamQuaID(id);
return View("ChiTietSanPham", ctsp);
}
}
}
2.5 Tìm hiu v Views
Trong controller HomeController.cs, c c Index() và About() u tr v mt view. Mt view cha
các th c tr v cho browser. Trong mô hình MVC thì mng vi mt trang .aspx
trong WebForm.
View ph c t ng dn. Ví d vi controller HomeController.cs ng
HomeController.Index() tr v mt view nm v ng dn \Views\Home\Index.aspx và
HomeController.About() s tr v mt view nm v ng dn \Views\Home\About.aspx
View About.aspx
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
AutoEventWireup="true" CodeBehind="About.aspx.cs"
Inherits="HiTest.Views.Home.About" %>
Microsoft Vietnam – DPE Team |Bài số 3: Tổng quan về ASP.NET MVC Framework
17
folder Views cli
.
View (Figure 14) ()
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
AutoEventWireup="true" CodeBehind="DanhSachSanPham.aspx.cs"
Inherits="BanHang.Views.SanPham.DanhSachSanPham" %>
<asp:Content ID="viewDanhSachSanPham" ContentPlaceHolderID="MainContent"
runat="server">
<h1>Đây là danh sách sản phẩm có trong chuyên mục</h1>
</asp:Content>
ChiTietSanPham.aspx
Microsoft Vietnam – DPE Team |Bài số 3: Tổng quan về ASP.NET MVC Framework
18
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
AutoEventWireup="true" CodeBehind="ChiTietSanPham.aspx.cs"
Inherits="BanHang.Views.SanPham.ChiTietSanPham" %>
<asp:Content ID="viewChiTietSanPham" ContentPlaceHolderID="MainContent"
runat="server">
<h1>Đây là Chi tiết sản phẩm</h1>
</asp:Content>
2.6 Tìm hiu v Models
Mt model trong ng dng ASP.NET MVC cha tt c các nghip v logic mà không có trong controllers và
views. Models cha tt c các tng truy xut d liu logic và tng nghip v logic. Ví d, nu s dng LINQ to
truy nhp d liu thì phi to nh dng dbml ) trong folder Models.
( Figure 15 )
namespace BanHang.Models
{
Microsoft Vietnam – DPE Team |Bài số 3: Tổng quan về ASP.NET MVC Framework
19
partial class DataClassesDataContext
{
public List<LoaiSanPham> LayCacLoaiSanPham()
{
return LoaiSanPhams.ToList();
}
public List<SanPham> LaySanPhamTuLoaiSanPham(string loaisanpham)
{
return SanPhams.Where(l => l.LoaiSanPham1.Id == loaisanpham).ToList();
}
public SanPham LaySanPhamQuaID(int id)
{
return SanPhams.Single(s => s.Id == id);
}
}
}
, -
(DanhMucLoaiSanPham.aspx.cs trong folder Views):
Figure 16.
\DanhMucLoaiSanPham.aspx.cs
\DanhMucLoaiSanPham.
Eval("TenLoaiSanPham"), "SanPham") %>
</li>
</ItemTemplate>
</asp:ListView>
</asp:Content>
Views ch cha tt c nhng gì hin th i dùng trên browser, Controllers ch cha các logic mà tr v
i dùng hong t n action khác. Còn li tt c c vit trong Models.
2.7
Views\Shared\Site.Master
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs"
Inherits="BanHang.Views.Shared.Site" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"
<html xmlns="
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><%= Html.Encode(ViewData["Title"]) %></title>
<link href=" / /Content/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="page">
<div id="header">
<div id="title">
<h1>My Sample MVC Application</h1>
2.8
Figure 17.
g ASP.NET MVC
3 Câu hỏi ôn tập
Hỏi: Views ca MVC có th s dng AJAX, javascript không?
Đáp c. V bn ch t trang .aspx vi mô hình
WebForm vì th có th thc hin các tác v . Vì th vic s dng jQuery, ASP.NET AJAX, và javascript
u có th thc hic.
Hỏi: Xây dng ng dng vi ASP.NET MVC Framework thì trang web s chi xây dng trên
n
Đápn là th. Nu xây dng trang web vi nn tng WebForm mà s dng ít các controls, usercontrols