ASP.NET三层架构详解 如何实现三层架构(3)

using System; using Model; using IDAL; namespace BLL { /// <summary> /// Content 的摘要说明。 /// </summary> public class Content { public ContentInfo GetContentInfo(int id) { // 取得从数据访问层取得一个文章内容实例 IContent dal = DALFactory.Content.Create(); // 用DAL查找文章内容 return dal.GetContentInfo(id); } } }

7、Web项目

1)、Web.config:

<appSettings> <add key="SQLConnString" value="Data Source=localhost ;Persist Security info=True;Initial Catalog=newsDB; User ID=sa;Password= " /> <add key="WebDAL" value="SQLServerDAL" /> </appSettings>

2)、WebUI.aspx

<%@ Page language="c#" Codebehind="WebUI.aspx.cs" AutoEventWireup="false" Inherits="Web.WebUI" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" > <HTML> <HEAD> <title>WebUI</title> <meta Content="Microsoft Visual Studio .NET 7.1"> <meta Content="C#"> <meta content="JavaScript"> <meta content="http://schemas.microsoft.com/intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form method="post" runat="server"> <FONT">宋体"></FONT> <table> <tr> <td>&nbsp;</td> <td>&nbsp; <asp:Label runat="server"></asp:Label></td> </tr> <tr> <td>&nbsp;</td> <td>&nbsp; <asp:Label runat="server"></asp:Label></td> </tr> <tr> <td>&nbsp;</td> <td>&nbsp; <asp:Label runat="server"></asp:Label></td> </tr> <tr> <td>&nbsp;</td> <td>&nbsp;</td> </tr> <tr> <td>&nbsp;</td> <td>&nbsp;</td> </tr> <tr> <td>&nbsp;</td> <td>&nbsp;</td> </tr> <tr> <td>&nbsp;</td> <td>&nbsp;</td> </tr> <tr> <td>&nbsp;</td> <td>&nbsp;</td> </tr> <tr> <td>&nbsp;</td> <td>&nbsp; <asp:Label runat="server">Label</asp:Label></td> </tr> </table> </form> </body> </HTML>

3)、WebUI.aspx.cs后台调用显示:

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 BLL; using Model; namespace myWeb { /// <summary> /// WebForm1 的摘要说明。 /// </summary> public class WebUI : System.Web.UI.Page { protected System.Web.UI.WebControls.Label lblTitle; protected System.Web.UI.WebControls.Label lblDataTime; protected System.Web.UI.WebControls.Label lblContent; protected System.Web.UI.WebControls.Label lblMsg; private ContentInfo ci ; private void Page_Load(object sender, System.EventArgs e) { if(!Page.IsPostBack) { GetContent("1"); } } private void GetContent(string id) { int ID = WebComponents.CleanString.GetInt(id); Content c = new Content(); ci = c.GetContentInfo(ID); if(ci!=null) { this.lblTitle.Text = ci.Title; this.lblDataTime.Text = ci.AddDate.ToString("yyyy-MM-dd"); this.lblContent.Text = ci.Content; } else { this.lblMsg.Text = "没有找到这篇文章"; } } #region Web 窗体设计器生成的代码 override protected void OnInit(EventArgs e) { // // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。 // InitializeComponent(); base.OnInit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } }

4)、WebComponents项目
(1)CleanString.cs

using System; using System.Text; namespace myWeb.WebComponents { /// <summary> /// CleanString 的摘要说明。 /// </summary> public class CleanString { public static int GetInt(string inputString) { try { return Convert.ToInt32(inputString); } catch { return 0; } } public static string InputText(string inputString, int maxLength) { StringBuilder retVal = new StringBuilder(); // check incoming parameters for null or blank string if ((inputString != null) && (inputString != String.Empty)) { inputString = inputString.Trim(); //chop the string incase the client-side max length //fields are bypassed to prevent buffer over-runs if (inputString.Length > maxLength) inputString = inputString.Substring(0, maxLength); //convert some harmful symbols incase the regular //expression validators are changed for (int i = 0; i < inputString.Length; i++) { switch (inputString[i]) { case '"': retVal.Append("&quot;"); break; case '<': retVal.Append("&lt;"); break; case '>': retVal.Append("&gt;"); break; default: retVal.Append(inputString[i]); break; } } // Replace single quotes with white space retVal.Replace("'", " "); } return retVal.ToString(); } } }

以上就是ASP.NET三层架构的全部内容,希望对大家的学习有所帮助。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjwwww.html