ASP.NET中的MVC模式应用

王朝vc·作者佚名  2006-01-09
宽屏版  字体: |||超大  

ASP.NET中的MVC模式应用

引言:

当我在Web开发的过程中经历的很长时间后,我仍然没有找到一些很好的代码能够说明ASP.NET下的

MVC模式开发。我列举了一个简单的例子,它给出了一个很好的关于使用MVC的方法或过程。

关于MVC

在传统的应用程序里,一块单一的代码处理了所有的事情。但是用MVC,你可以将你的程序分成3格

相互间合作的部分:Model、View和Controller(对于一个特有的名字,个人认为不必要进行完全的

翻译,这样效果会更好)。View是用户可以看到的部分,它将数据进行格式化并且呈现在用户眼前。

然而,实际上,它并不包含数据。数据是属于Model。最后Controller处理用户提交的命令并且修

改Model。想知道关于MVC个功能多的内容请打开下面的链接:

http://www.uidesign.net/1999/papers/webmvc_part1.html

Model:

也许你对MVC很了解,但是我仍给出了一个简单的代码,它显示如何执行了在ASP.NET中MVC的设计。

Model是你将进行所有的业务逻辑处理的部分。下面的类是仅仅增加了两个数字并且把结果送回。

<---------------------------------------------------------------------------------->

using System;

namespace MVCTest

{

/// This class is where we have the business logic build in.

/// It is a managed library that will be referenced by

/// your web application

public class Model

{

public Model()

{

}

// static method for adding two numbers

//

// @params int a,b - numbers to be added

// @return c - result

public static int Add(int a, int b)

{

int c = a + b;

return c;

}

// static nethod to add two numbers

//

// @params string a,b - numbers to be added

// @return int c - result

public static int Add(string a, string b)

{

int c = Int32.Parse(a) + Int32.Parse(b);

return c;

}

}

}

<---------------------------------------------------------------------------------->

Controller:

在ASP.Ner中,你通常会直接访问服务端。代替服务端是一个潜在的Controller,它是主要的进入口。

这将不提供的是一个中控中心,如果你首先想要使用服务端,或者任何的公共处理过程将会再发生

在所有的服务端。然而,你所有的code-behind的页面都继承子一个公共的类那就是

System.Web.UI.Page,你可以把所有的初始化的代码都放在OnLoad()的事件中,这是重载自每当页面

在任何时候被读取的时候都会被框架进行调用的页面类的OnLoad()。

现在,在你的代码的任何地方,你都可以重新定位并连接到一个新的aspx页面。你可以通过调用

getServicePage()的方法来获得将要跳转的页面的名字。

如果你想要重命名一个aspx页面文件,你就可以马上修改该文件的名字,并且改变getServicePage那你

的站点就仍然可以工作

视图

这部分是我们所有的aspx页面并且和它所需要的对aspx进行支持的code-behind类。这下面的例子中,我

写了2个aspx页面,一个用来得到用户的输入进而可以计算书2个数字的和,而另一个则输出结果。

这是View1.aspx页面的:

<%@ Page language="c#" Codebehind="View1.aspx.cs"

AutoEventWireup="false" Inherits="MVCApplication.View1" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<html>

<head>

<title>View1</title>

</head>

<body MS_POSITIONING="GridLayout">

<h3>ADDITION</h3>

<form runat="server" ID="Form" method="post">

<table>

<tr valign="middle">

<td class="field">First Number</td>

<td>

<asp:textbox id="one" runat="server" columns="32" maxlength="32" />

</td>

</tr>

<tr valign="middle">

<td class="field">Second Number</td>

<td>

<asp:textbox id="two" runat="server" columns="16" maxlength="16" />

</td>

</tr>

<tr>

<td colspan="2" align="center">

<asp:button id="btnSubmit" runat="server"

text="Add" onClick="Submit" cssclass="button" />

</td>

</tr>

</table>

</form>

</body>

</html>

这是View1.aspx.cs的code-behind类:

using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using MVCTest;

namespace MVCApplication

{

/// This is our View in the MVC model.

/// This class is a sub class of Controller.

/// This class gets the input from the View1.aspx

/// page, calls the add method in

/// Model class and sends to the next aspx page.

public class View1 : Controller

{

/// The next screen to send on the case of success.

/// Id of the screen as is in the Controller

private int Next_Screen = 2;

/// The screen to send on the case of failure/error.

/// Id of the screen as is in the Controller

private int Failure_Screen= 3;

protected TextBox one;

protected TextBox two;

protected HtmlForm Form;

protected Button btnSubmit;

public View1()

{

ServiceId = 1;

}

/// Get the number to be added from the user, perform the operation

/// and send the result.

protected void Submit(object sender, System.EventArgs e)

{

if(Page.IsValid)

{

string first = one.Text;

string second = two.Text;

int sum = Model.Add(first, second);

//Get the page name from the Controller

string page_path = GetServicePage(Next_Screen);

Server.Transfer(page_path+".aspx?sum="+ sum.ToString());

}else {

//On failure direct to this screen

Server.Transfer(GetServicePage(Failure_Screen));

}

}

private void Page_Load(object sender, System.EventArgs e)

{

}

}

}

下面是View2.aspx页面:

此页通过一个请求参数得到结果并且显示它。

<%@ Page language="c#" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >

<%@ Import Namespace="System.Web" %>

<%@ Import Namespace="System.Web.SessionState" %>

<%@ Import Namespace="System.Web.UI" %>

<%@ Import Namespace="System.Web.UI.WebControls" %>

<%@ Import Namespace="System.Web.UI.HtmlControls" %>

<script language="C#" runat="server">

void Page_Load(Object sender, EventArgs e)

{

Label1.Text = Request.Params.Get("sum");

}

</script>

<html>

<head>

<title>View2</title>

</head>

<body MS_POSITIONING="GridLayout">

<h3>SUM</h3>

<asp:Label id="Label1" Runat="server"></asp:Label>

<form id="View2" method="post" runat="server">

</form>

</body>

</html>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有