LoveUnix » 行业应用 项目实施 » Mvc模式在asp.net中的应用
让LU留住您的每

一天 让LU博客留住您的每一天
2003-10-21 21:53 threehair
zlyperson(翻译) <br /><br />关键字 MVC 模式 asp.net <br /><br />出处 <a href='http://www.codeproject.com/useritems/mvcasp.asp' target='_blank'>http://www.codeproject.com/useritems/mvcasp.asp</a> <br /><img src='http://www.sawin.com.cn/doc/SD/Design/mvc.GIF' border='0' alt='user posted image' /><br />介绍 <br />在网上搜寻了很长时间后却不能够找到一个演示ASP.NET的MVC模型的例子. 于是我实现了一个很好的能够领略MVC模型的简单实例.<br /><br />有关 MVC<br />在一个传统的应用程序中,一个单一代码要处理所有事物。 藉由MVC模型,你可以将你的应用程序有机的分成三个协作的部份: 模型,视图和控制器。视图是直接面向用户使用的部份。它格式化数据使数据以各种形式展现在荧屏上。然而实际上,它不包含数据。数据包容在模型中。最后,控制器部分接受用户操作命令进而<br /><br />修正模型内的数据。更多的有关MVC方面的知识可以到下面的连接<br /><a href='http://www.uidesign.net/1999/papers/webmvc_part1.html' target='_blank'>http://www.uidesign.net/1999/papers/webmvc_part1.html</a> <br /><br />模型<br />假定你知道MVC模型, 我要给你的这一个例子将演示如何在ASP.NET中实现MVC模式。<br /><br />模型是你所有的商业逻辑代码片段所在。我这里给出一个类实现二个数字相加,并把结果送回用户界面。 <br /><br />using System;<br /><br />namespace MVCTest<br />{<br />/// This class is where we have the business logic build in.<br />/// It is a managed library that will be referenced by <br />/// your web application<br /><br />public class Model<br />{<br />public Model()<br />{ <br />}<br /><br />// static method for adding two numbers<br />//<br />// @params int a,b - numbers to be added<br />// @return c - result<br />public static int Add(int a, int <!--emo&B)--><img src='style_emoticons/default/cool.gif' border='0' style='vertical-align:middle' alt='cool.gif' /><!--endemo--> <br />{<br />int c = a + b;<br /><br />return c;<br />}<br /><br />// static nethod to add two numbers<br />//<br />// @params string a,b - numbers to be added<br />// @return int c - result<br />public static int Add(string a, string <!--emo&B)--><img src='style_emoticons/default/cool.gif' border='0' style='vertical-align:middle' alt='cool.gif' /><!--endemo--><br />{<br />int c = Int32.Parse(a) + Int32.Parse(<!--emo&B)--><img src='style_emoticons/default/cool.gif' border='0' style='vertical-align:middle' alt='cool.gif' /><!--endemo-->;<br /><br />return c;<br />}<br /><br />}<br />}<br /><br /><br />控制器<br />在ASP.NET中,你通常直接地请求服务。取代身为子控制器的一个服务,它是我们进入ASP.NET应用程序的主要切入点。这里不提供的一个中心的位置判断你是否有权限使用该服务, 或者也不提供任何其他的通常应该在所有的服务中发生的处理。然而, 使你所有的与aspx关联的各个类继承一个通用的System.Web.UI.Page子类, 你就可以放置这些全部的预先处理代码到覆盖了Page类的OnLoad()方法中。OnLoad()会在每次页面被装载的时候被.Net框架调用。在你的与aspx代码关联的类中调用Page_Load()方法之前, 这里可以让你作为一个中心位置验证服务是否被允许使用以及做一些重定向, 这里你可以使用Server.Transfer()就好象真的中央控制器一样。我命名了我的视图控制类Controller。一旦你必须在显示任何的aspx页面之前验证登录信息或为用户做一些初始的操作, 那么这些都可以组合到OnLoad()方法中。<br /><br />现在,不管什么在地方当你的在应用程序中要重定向或者连接到一个aspx页面时候,你将使用GetServicePage()方法取得要跳转的文件名字。如果你已经重新命名网页来重整你的网站,你立刻会发现这样意义深远的简化了维护工作。如果要你重新命名一个aspx文件, 现在你可以重新命名该文件并改<br /><br />变GetServicePage的中的命名对, 这样你的网站仍能继续工作。<br /><br /><br /><br />using System;<br />using System.Collections;<br />using System.ComponentModel;<br />using System.Data;<br />using System.Drawing;<br />using System.Web;<br />using System.Web.SessionState;<br />using System.Web.UI;<br />using System.Web.UI.WebControls;<br />using System.Web.UI.HtmlControls;<br /><br />namespace MVCApplication<br />{<br /><br />/// This class acts as a controller extends System.Web.UI.Page. This class<br />/// maintains all the information about your web pages and performs any pre-requiste<br />/// conditions.<br /><br />public class Controller : System.Web.UI.Page<br />{<br />private int id = 0;<br /><br />//enum ViewPages has the information of all the aspx pages in your site<br />// it is represented as a name value pair.<br />public enum ViewPages<br />{<br />View1 = 1, View2, View3<br />}<br /><br />public int ServiceId <br />{<br />get<br />{<br />return id;<br />}<br /><br />set<br />{<br />id = value;<br />}<br /><br />}<br /><br />/// Method used for getting the name of the page by providing<br />/// its reference number.<br />/// @param int page_id - a number to retrieve the page name.<br />/// @return string page - name of the aspx page.<br />public string GetServicePage(int page_id) <br />{<br />//Get the page name by passing the number from enum<br />string page = Enum.GetName(typeof(ViewPages), page_id);<br /><br />return page;<br /><br />}<br /><br />// Could implement pre-requiste condition in this method<br />override protected void OnLoad(EventArgs e)<br />{<br />}<br /><br />private void Page_Load(object sender, System.EventArgs e)<br />{<br />}<br /><br /><br />#region Web Form Designer generated code<br />override protected void OnInit(EventArgs e)<br />{<br />//<br />// CODEGEN: This call is required by the ASP.NET Web Form Designer.<br />//<br />InitializeComponent();<br />base.OnInit(e);<br />}<br /><br />/// &lt;summary&gt;<br />/// Required method for Designer support - do not modify<br />/// the contents of this method with the code editor.<br />/// &lt;/summary&gt;<br />private void InitializeComponent()<br />{ <br />this.Load += new System.EventHandler(this.Page_Load);<br />}<br />#endregion<br />}<br />}<br /><br />视图<br />这里有我们所有的aspx页面及其支援的对应类代码片段。例子中我使用了2个aspx页面。一个是为取得用户为使用两个数字相加而输入的两个数字,而另一个则为用户返回显示结果。<br /><br /><br />这里是View1.aspx页面的原代码<br />-----------------------------------<br />&lt;%@ Page language=&quot;c#&quot; Codebehind=&quot;View1.aspx.cs&quot; AutoEventWireup=&quot;false&quot; <br /><br />Inherits=&quot;MVCApplication.View1&quot; %&gt;<br />&lt;&#33;DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot; &gt;<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;View1&lt;/title&gt; <br />&lt;/head&gt;<br />&lt;body MS_POSITIONING=&quot;GridLayout&quot;&gt;<br />&lt;h3&gt;ADDITION&lt;/h3&gt;<br />&lt;form runat=&quot;server&quot; ID=&quot;Form&quot; method=&quot;post&quot;&gt;<br />&lt;table&gt;<br />&lt;tr valign=&quot;middle&quot;&gt;<br />&lt;td class=&quot;field&quot;&gt;First Number&lt;/td&gt;<br />&lt;td&gt;<br />&lt;asp:textbox id=&quot;one&quot; runat=&quot;server&quot; <br /><br />columns=&quot;32&quot; maxlength=&quot;32&quot; /&gt;<br />&lt;/td&gt;<br />&lt;/tr&gt;<br />&lt;tr valign=&quot;middle&quot;&gt;<br />&lt;td class=&quot;field&quot;&gt;Second Number&lt;/td&gt;<br />&lt;td&gt;<br />&lt;asp:textbox id=&quot;two&quot; runat=&quot;server&quot; <br /><br />columns=&quot;16&quot; maxlength=&quot;16&quot; /&gt;<br />&lt;/td&gt;<br />&lt;/tr&gt;<br />&lt;tr&gt;<br />&lt;td colspan=&quot;2&quot; align=&quot;center&quot;&gt;<br />&lt;asp:button id=&quot;btnSubmit&quot; runat=&quot;server&quot; <br /><br />text=&quot;Add&quot; onClick=&quot;Submit&quot; cssclass=&quot;button&quot; /&gt;<br />&lt;/td&gt;<br />&lt;/tr&gt;<br />&lt;/table&gt;<br />&lt;/form&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br /><br />这下面是View1.aspx.cs的代码<br />---------------------------------------------------<br /><br />using System;<br />using System.Collections;<br />using System.ComponentModel;<br />using System.Data;<br />using System.Drawing;<br />using System.Web;<br />using System.Web.SessionState;<br />using System.Web.UI;<br />using System.Web.UI.WebControls;<br />using System.Web.UI.HtmlControls;<br /><br />using MVCTest;<br /><br />namespace MVCApplication<br />{<br />/// This is our View in the MVC model. This class is a sub class of Controller.<br />/// This class gets the input from the View1.aspx page, calls the add method in <br />/// Model class and sends to the next aspx page.<br />public class View1 : Controller<br />{<br /><br />/// The next screen to send on the case of success. <br />/// Id of the screen as is in the Controller<br />private int Next_Screen = 2;<br /><br />/// The screen to send on the case of failure/error.<br />/// Id of the screen as is in the Controller<br />private int Failure_Screen= 3;<br /><br />protected TextBox one;<br />protected TextBox two;<br /><br />protected HtmlForm Form;<br />protected Button btnSubmit;<br /><br />public View1() <br />{<br />ServiceId = 1;<br />}<br /><br />/// Get the number to be added from the user, perform the operation<br />/// and send the result.<br />protected void Submit(object sender, System.EventArgs e)<br />{ <br />if(Page.IsValid) <br />{<br />string first = one.Text;<br />string second = two.Text;<br /><br />int sum = Model.Add(first, second);<br /><br />//Get the page name from the Controller<br />string page_path = GetServicePage(Next_Screen);<br />Server.Transfer(page_path+&quot;.aspx?sum=&quot;+ sum.ToString());<br /><br />}else {<br />//On failure direct to this screen<br />Server.Transfer(GetServicePage(Failure_Screen));<br />}<br />}<br /><br />private void Page_Load(object sender, System.EventArgs e)<br />{<br />}<br /><br />#region Web Form Designer generated code<br />override protected void OnInit(EventArgs e)<br />{<br />//<br />// CODEGEN: This call is required by the ASP.NET Web Form Designer.<br />//<br />InitializeComponent();<br />base.OnInit(e);<br />}<br /><br />/// &lt;summary&gt;<br />/// Required method for Designer support - do not modify<br />/// the contents of this method with the code editor.<br />/// &lt;/summary&gt;<br />private void InitializeComponent()<br />{ <br />this.Load += new System.EventHandler(this.Page_Load);<br />}<br />#endregion<br />}<br />}<br /><br />以下是View2.aspx的原文件<br />----------------------<br /><br />This page gets the result as a request parameter and displays it.<br /><br />&lt;%@ Page language=&quot;c#&quot; %&gt;<br />&lt;&#33;DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot; &gt;<br />&lt;%@ Import Namespace=&quot;System.Web&quot; %&gt;<br />&lt;%@ Import Namespace=&quot;System.Web.SessionState&quot; %&gt;<br />&lt;%@ Import Namespace=&quot;System.Web.UI&quot; %&gt;<br />&lt;%@ Import Namespace=&quot;System.Web.UI.WebControls&quot; %&gt;<br />&lt;%@ Import Namespace=&quot;System.Web.UI.HtmlControls&quot; %&gt;<br />&#60;script language=&quot;C#&quot; runat=&quot;server&quot;&gt;<br />void Page_Load(Object sender, EventArgs e)<br />{<br />Label1.Text = Request.Params.Get(&quot;sum&quot;); <br />}<br />&lt;/script&gt;<br />&lt;html&gt;<br />&lt;head&gt;<br />&lt;title&gt;View2&lt;/title&gt; <br />&lt;/head&gt;<br />&lt;body MS_POSITIONING=&quot;GridLayout&quot;&gt;<br />&lt;h3&gt;SUM&lt;/h3&gt;<br />&lt;asp:Label id=&quot;Label1&quot; Runat=&quot;server&quot;&gt;&lt;/asp:Label&gt;<br />&lt;form id=&quot;View2&quot; method=&quot;post&quot; runat=&quot;server&quot;&gt;<br />&lt;/form&gt;<br />&lt;/body&gt;<br />&lt;/html&gt;<br /><br />结论<br />我希望这一个例子将会帮助你体会MVC模型的架构。这里提供的例子代码仅仅是演示如何实现这一模型架构, 而在实际的应用中通常会用一个配置文件来替换这个固定的GetServerPage(), 我之所以选择了这个只是为了保持代码的简洁。

2003-10-21 21:56 mygod
还不错,不过太简单了,希望能有更深入一点的

页: [1]


Powered by Discuz! Archiver 5.5.0  © 2001-2006 Comsenz Inc.