Building CMS / E-Commerce Project using ASP.NET 3.5 in C# 2008 and SQLServer 2005.Chương 3 docx

11 440 0
Building CMS / E-Commerce Project using ASP.NET 3.5 in C# 2008 and SQLServer 2005.Chương 3 docx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Building CMS / E-Commerce Project using ASP.NET 3.5 in C# 2008 and SQLServer 2005 Chương Planning an Architecture *** Tạo lớp ConfigSection.cs thư mục App_Code viết code sau: namespace KimSoft { /// /// Lớp KimSoftSection kế thừa từ lớp ConfigurationSection /// Chứa thuộc tính cấu hình website phương thức (get, set) tương ứng /// public class KimSoftSection : ConfigurationSection { //Chuỗi kết nối liệu [ConfigurationProperty("defaultConnectionStringName", DefaultValue = "LocalSqlServer")] public string DefaultConnectionStringName { get { return (string)base["defaultConnectionStringName"]; } set { base["connectionStdefaultConnectionStringNameringName"] = value; } } //Thời gian Cache liệu (đơn vị giây) [ConfigurationProperty("defaultCacheDuration", DefaultValue = "600")] public int DefaultCacheDuration { get { return (int)base["defaultCacheDuration"]; } set { base["defaultCacheDuration"] = value; } } //Các thuộc tính trang Contact.aspx [ConfigurationProperty("contactForm", IsRequired = true)] public ContactFormElement ContactForm { get { return (ContactFormElement)base["contactForm"]; } } } /// /// Lớp ContactFormElement kế thừa từ lớp ConfigurationElement /// chứa thuộc tính gói mail /// public class ContactFormElement : ConfigurationElement { //Chủ đề [ConfigurationProperty("mailSubject", DefaultValue = "Mail from KimSoft: {0}")] public string MailSubject { get { return (string)base["mailSubject"]; } set { base["mailSubject"] = value; } } GVHD: Dương Ngọc Long Nam – longnamit@yahoo.com Page Building CMS / E-Commerce Project using ASP.NET 3.5 in C# 2008 and SQLServer 2005 //Email nhận [ConfigurationProperty("mailTo", IsRequired = true)] public string MailTo { get { return (string)base["mailTo"]; } set { base["mailTo"] = value; } } //Email CC [ConfigurationProperty("mailCC")] public string MailCC { get { return (string)base["mailCC"]; } set { base["mailCC"] = value; } } //Email gởi (mailFrom lưu file web.config) } } Lớp KimSoftSection ánh xạ với section file web.config sau:  Khai báo section tên KimSoft configSection  Định nghĩa section KimSoft GVHD: Dương Ngọc Long Nam – longnamit@yahoo.com Page Building CMS / E-Commerce Project using ASP.NET 3.5 in C# 2008 and SQLServer 2005 Thêm vào public field of type KimSoftSection bên lớp Globals sau: namespace KimSoft { public static class Globals { public static string ThemesSelectorID = ""; //Khai báo khởi tạo biến tĩnh (static) đọc (readonly) Settings kiểu KimSoftSection // Giá trị khởi tạo lấy từ file web.config thông qua lớp WebConfigurationManager public readonly static KimSoftSection Settings = (KimSoftSection)WebConfigurationManager.GetSection("KimSoft"); } } Thêm vào namespace System.Web.Configuration cho WebConfigurationManager using System.Web.Configuration;//WebConfigurationManager Thêm code vào trang Contact.aspx sau: Your name: * Your e-mail: * * Subject: * Body: * Viết code cho kiện txtSubmit_Click GVHD: Dương Ngọc Long Nam – longnamit@yahoo.com Page Building CMS / E-Commerce Project using ASP.NET 3.5 in C# 2008 and SQLServer 2005  Double-click vào button Send để phát sinh kiện txtSubmit_Click  Viết code cho kiện txtSubmit_Click sau: namespace KimSoft.UI { public partial class Contact : BasePage { protected void Page_Load(object sender, EventArgs e){ } protected void txtSubmit_Click(object sender, EventArgs e) { try { // Khai báo gói tin (email) MailMessage msg = new MailMessage(); // Nội dung (Body) text thông thường code HTML msg.IsBodyHtml = false; // Email, tên người gởi lấy từ text box txtEmail txtName msg.From = new MailAddress(txtEmail.Text, txtName.Text); // Email người nhận lấy từ file web.config thông qua lớp Globals msg.To.Add(new MailAddress(Globals.Settings.ContactForm.MailTo)); // Nếu MailCC khác NULL hay khác rỗng thêm MailCC vào gói tin if (!string.IsNullOrEmpty(Globals.Settings.ContactForm.MailCC)) msg.CC.Add(new MailAddress(Globals.Settings.ContactForm.MailCC)); // Chủ đề lấy từ text box txtSubject msg.Subject = string.Format(Globals.Settings.ContactForm.MailSubject, txtSubject.Text); // Nội dung tin lấy từ text box txtBody msg.Body = txtBody.Text; // Khai báo khởi tạo biến SmtpClient SmtpClient smtp = new SmtpClient(); //Gởi gói tin smtp.Send(msg); // Hiển thị thông báo gởi mail thành công lblFeedbackOK.Visible = true; lblFeedbackKO.Visible = false; //Reset lại text box txtName.Text = ""; txtEmail.Text = ""; txtSubject.Text = ""; txtBody.Text = ""; } catch (Exception) { // Hiển thị thông báo gởi mail không thành công lblFeedbackOK.Visible = false; lblFeedbackKO.Visible = true; } } } }  Thêm vào thư viện: using System.Net.Mail;//MailMessage, MailAddress, SmtpClient GVHD: Dương Ngọc Long Nam – longnamit@yahoo.com Page Building CMS / E-Commerce Project using ASP.NET 3.5 in C# 2008 and SQLServer 2005 Chạy kiểm tra chương trình Thiết lập giao thức SMTP để send message, định nghĩa SMTP file web.config file sau: Cài đặt cấu hình SMTP Service (nếu máy bạn chưa có – Xem phụ lục kèm theo chương 3) 10 Tạo thư mục DAL thư mục App_Code/DAL 11 Tạo lớp DataAccess thư mục App_Code/DAL/DataAccess.cs Và viết code sau: namespace KimSoft.DAL { /// /// Lớp DataAccess lớp trừu tượng (abstract class) /// public abstract class DataAccess { //Thuộc tính chuỗi kết nối private string _connectionString = ""; protected string ConnectionString { get { return _connectionString; } set { _connectionString = value; } } //Thuộc tính EnableCaching(true, false) cho phép Caching hay khơng private bool _enableCaching = true; protected bool EnableCaching { get { return _enableCaching; } set { _enableCaching = value; } } //Thuộc tính CacheDuration private int _cacheDuration = 0; protected int CacheDuration { get { return _cacheDuration; } set { _cacheDuration = value; } } //Trả Cache protected Cache Cache { GVHD: Dương Ngọc Long Nam – longnamit@yahoo.com Page Building CMS / E-Commerce Project using ASP.NET 3.5 in C# 2008 and SQLServer 2005 get { return HttpContext.Current.Cache; } } /// /// Thực thi DML store /// /// /// protected int ExecuteNonQuery(DbCommand cmd) { if (HttpContext.Current.User.Identity.Name.ToLower() == "sampleeditor") { foreach (DbParameter param in cmd.Parameters) { if (param.Direction == ParameterDirection.Output || param.Direction == ParameterDirection.ReturnValue) { switch (param.DbType) { case DbType.AnsiString: case DbType.AnsiStringFixedLength: case DbType.String: case DbType.StringFixedLength: case DbType.Xml: param.Value = ""; break; case DbType.Boolean: param.Value = false; break; case DbType.Byte: param.Value = byte.MinValue; break; case DbType.Date: case DbType.DateTime: param.Value = DateTime.MinValue; break; case DbType.Currency: case DbType.Decimal: param.Value = decimal.MinValue; break; case DbType.Guid: param.Value = Guid.Empty; break; case DbType.Double: case DbType.Int16: case DbType.Int32: case DbType.Int64: param.Value = 0; break; default: param.Value = null; break; } } } return 1; } else GVHD: Dương Ngọc Long Nam – longnamit@yahoo.com Page Building CMS / E-Commerce Project using ASP.NET 3.5 in C# 2008 and SQLServer 2005 return cmd.ExecuteNonQuery(); } /// /// Thực thi câu lệnh select store behavior mặc định /// trả danh sách đối tượng lưu IDataReader /// /// /// protected IDataReader ExecuteReader(DbCommand cmd) { return ExecuteReader(cmd, CommandBehavior.Default); } /// /// Thực thi câu lệnh select store theo tham số behavior /// trả danh sách đối tượng lưu IDataReader /// /// /// /// protected IDataReader ExecuteReader(DbCommand cmd, CommandBehavior behavior) { return cmd.ExecuteReader(behavior); } /// /// Thực thi câu lệnh select store trả giá trị /// /// /// object protected object ExecuteScalar(DbCommand cmd) { return cmd.ExecuteScalar(); } } } 12 Thêm vào namespace sau cho Cach DbCommand: using System.Web.Caching;//Cache using System.Data.Common;//DbCommand 13 Tạo thư mục BLL thư mục App_Code 14 Tạo lớp BizObject.cs thư mục App_Data/BLL/ định nghĩa số thuộc tính, phương thức thường xuyên sử dụng đối tượng khác BLL sau: namespace KimSoft.BLL { public abstract class BizObject { // Hằng số (const) MAXROWS // int.MaxValue = 65.536 protected const int MAXROWS = int.MaxValue; GVHD: Dương Ngọc Long Nam – longnamit@yahoo.com Page Building CMS / E-Commerce Project using ASP.NET 3.5 in C# 2008 and SQLServer 2005 // Trả Cache protected static Cache Cache { get { return HttpContext.Current.Cache; } } //Lấy thông tin User protected static IPrincipal CurrentUser { get { return HttpContext.Current.User; } } //Lấy tên User protected static string CurrentUserName { get { string userName = ""; if (HttpContext.Current.User.Identity.IsAuthenticated) userName = HttpContext.Current.User.Identity.Name; return userName; } } //Lấy IP User protected static string CurrentUserIP { get { return HttpContext.Current.Request.UserHostAddress; } } /// /// Trả số trang /// /// /// /// int protected static int GetPageIndex(int startRowIndex, int maximumRows) { if (maximumRows

Ngày đăng: 02/07/2014, 00:20

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan