MVC模型的简单实例.
有关 MVC
在一个传统的应用程序中,一个单一代码要处理所有事物。 藉由MVC模型,你可以将你的应用程序有机的分成三个协作的部份: 模型,视图和控制器。视图是直接面向用户使用的部份。它格式化数据使数据以各种形式展现在荧屏上。然而实际上,它不包含数据。数据包容在模型中。最后,控制器部分接受用户操作命令进而
修正模型内的数据。更多的有关MVC方面的知识可以到下面的连接
模型
假定你知道MVC模型, 我要给你的这一个例子将演示如何在ASP.NET中实现MVC模式。
模型是你所有的商业逻辑代码片段所在。我这里给出一个类实现二个数字相加,并把结果送回用户界面。
using System;2

3
namespace MVCTest4
{5
/// This class is where we have the business logic build in.6
/// It is a managed library that will be referenced by 7
/// your web application8

9
public class Model10
{11
public Model()12
{ 13
}14
15
// static method for adding two numbers16
//17
// @params int a,b - numbers to be added18
// @return c - result19
public static int Add(int a, int b) 20
{21
int c = a + b;22

23
return c;24
}25

26
// static nethod to add two numbers27
//28
// @params string a,b - numbers to be added29
// @return int c - result30
public static int Add(string a, string b)31
{32
int c = Int32.Parse(a) + Int32.Parse(b);33

34
return c;35
}36

37
}38
}39

40

控制器
在ASP.NET中,你通常直接地请求服务。取代身为子控制器的一个服务,它是我们进入ASP.NET应用程序的主要切入点。这里不提供的一个中心的位置判断你是否有权限使用该服务, 或者也不提供任何其他的通常应该在所有的服务中发生的处理。然而, 使你所有的与aspx关联的各个类继承一个通用的System.Web.UI.Page子类, 你就可以放置这些全部的预先处理代码到覆盖了Page类的OnLoad()方法中。OnLoad()会在每次页面被装载的时候被.Net框架调用。在你的与aspx代码关联的类中调用Page_Load()方法之前, 这里可以让你作为一个中心位置验证服务是否被允许使用以及做一些重定向, 这里你可以使用Server.Transfer()就好象真的中央控制器一样。我命名了我的视图控制类Controller。一旦你必须在显示任何的aspx页面之前验证登录信息或为用户做一些初始的操作, 那么这些都可以组合到OnLoad()方法中。
现在,不管什么在地方当你的在应用程序中要重定向或者连接到一个aspx页面时候,你将使用GetServicePage()方法取得要跳转的文件名字。如果你已经重新命名网页来重整你的网站,你立刻会发现这样意义深远的简化了维护工作。如果要你重新命名一个aspx文件, 现在你可以重新命名该文件并改
变GetServicePage的中的命名对, 这样你的网站仍能继续工作。
using System;2
using System.Collections;3
using System.ComponentModel;4
using System.Data;5
using System.Drawing;6
using System.Web;7
using System.Web.SessionState;8
using System.Web.UI;9
using System.Web.UI.WebControls;10
using System.Web.UI.HtmlControls;11

12
namespace MVCApplication13
{14
15
/// This class acts as a controller extends System.Web.UI.Page. This class16
/// maintains all the information about your web pages and performs any pre-requiste17
/// conditions.18
19
public class Controller : System.Web.UI.Page20
{21
private int id = 0;22

23
//enum ViewPages has the information of all the aspx pages in your site24
// it is represented as a name value pair.25
public enum ViewPages26
{27
View1 = 1, View2, View328
}29

30
public int ServiceId 31
{32
get33
{34
return id;35
}36

37
set38
{39
id = value;40
}41

42
}43

44
/// Method used for getting the name of the page by providing45
/// its reference number.46
/// @param int page_id - a number to retrieve the page name.47
/// @return string page - name of the aspx page.48
public string GetServicePage(int page_id) 49
{50
//Get the page name by passing the number from enum51
string page = Enum.GetName(typeof(ViewPages), page_id);52

53
return page;54

55
}56

57
// Could implement pre-requiste condition in this method58
override protected void OnLoad(EventArgs e)59
{60
}61

62
private void Page_Load(object sender, System.EventArgs e)63
{64
}65

66

67
Web Form Designer generated code86
}87
}88

视图
这里有我们所有的aspx页面及其支援的对应类代码片段。例子中我使用了2个aspx页面。一个是为取得用户为使用两个数字相加而输入的两个数字,而另一个则为用户返回显示结果。
这里是View1.aspx页面的原代码
-----------------------------------
<%@ Page language="c#" Codebehind="View1.aspx.cs" AutoEventWireup="false" 2

3
Inherits="MVCApplication.View1" %>4
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >5
<html>6
<head>7
<title>View1</title> 8
</head>9
<body MS_POSITIONING="GridLayout">10
<h3>ADDITION</h3>11
<form runat="server" ID="Form" method="post">12
<table>13
<tr valign="middle">14
<td class="field">First Number</td>15
<td>16
<asp:textbox id="one" runat="server" 17

18
columns="32" maxlength="32" />19
</td>20
</tr>21
<tr valign="middle">22
<td class="field">Second Number</td>23
<td>24
<asp:textbox id="two" runat="server" 25

26
columns="16" maxlength="16" />27
</td>28
</tr>29
<tr>30
<td colspan="2" align="center">31
<asp:button id="btnSubmit" runat="server" 32

33
text="Add" onClick="Submit" cssclass="button" />34
</td>35
</tr>36
</table>37
</form>38
</body>39
</html>40

这下面是View1.aspx.cs的代码
---------------------------------------------------
using System;2
using System.Collections;3
using System.ComponentModel;4
using System.Data;5
using System.Drawing;6
using System.Web;7
using System.Web.SessionState;8
using System.Web.UI;9
using System.Web.UI.WebControls;10
using System.Web.UI.HtmlControls;11

12
using MVCTest;13

14
namespace MVCApplication15
{16
/// This is our View in the MVC model. This class is a sub class of Controller.17
/// This class gets the input from the View1.aspx page, calls the add method in 18
/// Model class and sends to the next aspx page.19
public class View1 : Controller20
{21

22
/// The next screen to send on the case of success. 23
/// Id of the screen as is in the Controller24
private int Next_Screen = 2;25

26
/// The screen to send on the case of failure/error.27
/// Id of the screen as is in the Controller28
private int Failure_Screen= 3;29

30
protected TextBox one;31
protected TextBox two;32

33
protected HtmlForm Form;34
protected Button btnSubmit;35

36
public View1() 37
{38
ServiceId = 1;39
}40

41
/// Get the number to be added from the user, perform the operation42
/// and send the result.43
protected void Submit(object sender, System.EventArgs e)44
{ 45
if(Page.IsValid) 46
{47
string first = one.Text;48
string second = two.Text;49

50
int sum = Model.Add(first, second);51
52
//Get the page name from the Controller53
string page_path = GetServicePage(Next_Screen);54
Server.Transfer(page_path+".aspx?sum="+ sum.ToString());55

56
}else {57
//On failure direct to this screen58
Server.Transfer(GetServicePage(Failure_Screen));59
}60
}61

62
private void Page_Load(object sender, System.EventArgs e)63
{64
}65

66
Web Form Designer generated code85
}86
}87

88
以下是View2.aspx的原文件89
----------------------90

91
This page gets the result as a request parameter and displays it.92

93
<%@ Page language="c#" %>94
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >95
<%@ Import Namespace="System.Web" %>96
<%@ Import Namespace="System.Web.SessionState" %>97
<%@ Import Namespace="System.Web.UI" %>98
<%@ Import Namespace="System.Web.UI.WebControls" %>99
<%@ Import Namespace="System.Web.UI.HtmlControls" %>100
<script language="C#" runat="server">101
void Page_Load(Object sender, EventArgs e)102
{103
Label1.Text = Request.Params.Get("sum"); 104
}105
</script>106
<html>107
<head>108
<title>View2</title> 109
</head>110
<body MS_POSITIONING="GridLayout">111
<h3>SUM</h3>112
<asp:Label id="Label1" Runat="server"></asp:Label>113
<form id="View2" method="post" runat="server">114
</form>115
</body>116
</html>117

结论
我希望这一个例子将会帮助你体会MVC模型的架构。这里提供的例子代码仅仅是演示如何实现这一模型架构, 而在实际的应用中通常会用一个配置文件来替换这个固定的GetServerPage(), 我之所以选择了这个只是为了保持代码的简洁。



浙公网安备 33010602011771号