Tải bản đầy đủ (.pdf) (11 trang)

Tài liệu Bài số 4: Định tuyến URL và điều phối hiển thị pptx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (956.65 KB, 11 trang )

Bài số 4

Định tuyến URL và điều phối hiển thị
Table of Contents
1 URL routing (Định tuyến URL) .............................................................................................. 2
1.1 Giới thiệu định tuyến URL ....................................................................................................... 2
1.1.1 Hệ thống định tuyến trong ASP.NET MVC làm gì?........................................................ 2
1.1.2 Các quy tắc định tuyến URL mặc định trong ASP.NET MVC Web Application ............. 2
1.2 Ví dụ định tuyến URL............................................................................................................... 3
2 Điều phối hiển thị dữ liệu .......................................................................................................... 6
2.1 Điều phối hiển thị dữ liệu với ViewData Dictionary ................................................................... 6
2.2 Điều phối hiển thị dữ liệu với cách dùng Strongly Typed Classes .............................................. 9
2.2.1 Li ích cua việc dùng strongly typed ............................................................................. 9
2.2.2 Tao strongly-typed DuLieuDanhSachSanPham trong folder Models ............................ 9
2.2.3 Dng ViewData diconary với một đối tưng ViewData strongly typed ................... 11
3 Câu hỏi ôn tập .......................................................................................................................... 11
4 Ti liệu tham khảo .................................................................................................................... 11



Microsoft Vietnam – DPE Team | Bài số 4: Định tuyến URL và điều phối hiển thị
2


1 URL routing (Định tuyến URL)
1.1 Giới thiệu định tuyến URL
1.1.1 H thnh tuyn trong ASP.NET MVC làm gì?
ASP.NET MVC Framework có mt h thnh tuyn URL ( URL Routing System ) linh hot cho phép xác
nh các quy tc ánh x a ch URL bên trong ng dng. Mt h thnh tuyn có 2 m
 Xây dng mt tp hp các URL ng dng nh tuyn chúng ti các Controller và thc thi các
 x lý.


 Xây dng các URL g có th gc tr li Controllers/Actions ( ví d: form posts, liên kt <a
i gi AJAX )
S dng các quy tc ánh x URL  u khi m do cho vic lp trình ng
dng, nu mui cu trúc URL ( ví d /Catalog thành /Products ) có th i mt tp hp
quy tc ánh x mc ng dng mà không cn phi vit li mã lp trình bên trong Controllers và Views.
1.1.2 Các quy tnh tuyn URL mnh trong ASP.NET MVC Web Application
Mnh khi to ng dng vi ASP.NET MVC Web Application trong Visual Studio s to ra mt ASP.NET
Application class gi là Global.asax cha cu hình các quy tnh tuyn URL. Xây dnh tuyn thông
  c RegisterRoutes(ReouteCollection routes) và khi ng dng b u,  c
Application_Start() trong Global.asax.cs s g to ra bnh tuyn.
Global.asax.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace BanHang
{
// 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
);

}

Microsoft Vietnam – DPE Team | Bài số 4: Định tuyến URL và điều phối hiển thị
3

protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
}
Mnh tuyn URL trong ASP.NET MVC Framework có cu trúc dng: Controllers/ControllerAction/Id
Vi ASP.NET MVC Web Application thì mnh Controllers là HomeController, mnh ControllerAction là
Index, m nh Id là r    i tran  c xây dng thông qua template ASP.NET Web
Application thì mnh http://localhost/  vi http://localhost/Home/Index/
Khi ng dng ASP.NET MVC Web Application nhc mt Url, MVC Framework s nh giá các quy tc
nh tuyn trong tp h quynh Controller nào s u khin request.
MVC framework chn Controller bnh giá các quy tc trong bnh tuyn theo trt t  sn.
1.2 Ví dụ định tuyến URL





 .NET MVC Web Application:
To TimKiem URL

Figure 1. To controller TimKiemController.cs
Microsoft Vietnam – DPE Team | Bài số 4: Định tuyến URL và điều phối hiển thị
4

Có 2 action trong TimKiem hin th mt trang search vi mi
dùng nhp t khóa c u khin khi yêu cu tìm kinh.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;

namespace BanHang.Controllers
{
public class TimKiemController : Controller
{
public ActionResult Index()
{
// Add action logic here
return View();
}

public ActionResult Results(string query)
{
return View();

}
}
}
Trong Global.asax.cs mt cách thnh tuyn mnh. Theo quy tnh tuyn mnh thì khi yêu cu mt
trang tìm kia ch c gi theo s ng vi [controller]/[action]/[id] là /TimKiem/Results/[string
query]. Cách dùng này không có v gu mt cách tùy binh tuy i thành
/TimKiem/[string query]. Thêm vào trong Global.asax.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace BanHang
{
// 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(
"TimKiem", // Route
name
"TimKiem/{query}", // URL with parameters
new { controller = "TimKiem", action = "Results" } // Parameter

defaults
);

routes.MapRoute(
"Default", // Route
name
"{controller}/{action}/{id}", // URL with
parameters
Microsoft Vietnam – DPE Team | Bài số 4: Định tuyến URL và điều phối hiển thị
5

new { controller = "Home", action = "Index", id = "" } //
Parameter defaults
);

}

protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
}
To 2 view hin th d liu khin trong TimKiemController.cs là Index.aspx và Results.aspx
Index.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
AutoEventWireup="true" CodeBehind="Index.aspx.cs"
Inherits="BanHang.Views.TimKiem.Index" %>
<asp:Content ID="viewTimKiem" ContentPlaceHolderID="MainContent" runat="server">
<form method="post" action="TimKiem/Search">

<input type="text" id="txtTimKiem" name="txtTimKiem" />
<input type="submit" value="Tìm Kiếm" />
</form>
</asp:Content>
Result.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
AutoEventWireup="true" CodeBehind="Results.aspx.cs"
Inherits="BanHang.Views.TimKiem.Results" %>
<asp:Content ID="viewResults" ContentPlaceHolderID="MainContent" runat="server">
Kết quả dữ liệu tìm kiếm được ở đây
</asp:Content>
Thêm vào Views\Shared\Site.master mt tab tìm kim
<%@ 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>

</div>

<div id="logindisplay">
<% Html.RenderPartial("LoginUserControl"); %>
</div>

<div id="menucontainer">

<ul id="menu">
<li><%= Html.ActionLink("Home", "Index", "Home")%></li>

×