Building CMS / E-Commerce Project using ASP.NET 3.5 in C# 2008 and SQLServer 2005.Chương 3
lượt xem 110
download

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

Chương 3: Planning an Architecture
Bình luận(0) Đăng nhập để gửi bình luận!
Nội dung Text: Building CMS / E-Commerce Project using ASP.NET 3.5 in C# 2008 and SQLServer 2005.Chương 3
- Building CMS / E-Commerce Project using ASP.NET 3.5 in C# 2008 and SQLServer 2005 Chương 3 Planning an Architecture *** 1. Tạo lớp ConfigSection.cs trong thư mục App_Code và viết code như sau: namespace KimSoft { /// /// Lớp KimSoftSection kế thừa từ lớp ConfigurationSection /// Chứa các thuộc tính cấu hình của website và các phương thức (get, set) tương ứng. /// public class KimSoftSection : ConfigurationSection { //Chuỗi kết nối dữ liệu [ConfigurationProperty("defaultConnectionStringName", DefaultValue = "LocalSqlServer")] public string DefaultConnectionStringName { get { return (string)base["defaultConnectionStringName"]; } set { base["connectionStdefaultConnectionStringNameringName"] = value; } } //Thời gian Cache dữ liệu (đơn vị là giây). [ConfigurationProperty("defaultCacheDuration", DefaultValue = "600")] public int DefaultCacheDuration { get { return (int)base["defaultCacheDuration"]; } set { base["defaultCacheDuration"] = value; } } //Các thuộc tính trong 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 các thuộc tính của một 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 1
- 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 được lưu trong file web.config). } } 2. Lớp KimSoftSection được ánh xạ với section trong file web.config như sau: Khai báo section tên KimSoft trong configSection Định nghĩa section KimSoft GVHD: Dương Ngọc Long Nam – longnamit@yahoo.com Page 2
- Building CMS / E-Commerce Project using ASP.NET 3.5 in C# 2008 and SQLServer 2005 3. Thêm vào public field of type KimSoftSection bên trong lớp Globals như sau: namespace KimSoft { public static class Globals { public static string ThemesSelectorID = ""; //Khai báo và khởi tạo biến tĩnh (static) chỉ đọc (readonly) Settings kiểu KimSoftSection // Giá trị khởi tạo được lấy từ file web.config thông qua lớp WebConfigurationManager public readonly static KimSoftSection Settings = (KimSoftSection)WebConfigurationManager.GetSection("KimSoft"); } } 4. Thêm vào namespace System.Web.Configuration cho WebConfigurationManager using System.Web.Configuration;//WebConfigurationManager 5. Thêm code vào trang Contact.aspx như sau: Your name: * Your e-mail: *
- Building CMS / E-Commerce Project using ASP.NET 3.5 in C# 2008 and SQLServer 2005 Display="dynamic" ID="valEmailPattern" SetFocusOnError="true" ControlToValidate="txtEmail" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" ErrorMessage="The e-mail address you specified is not well-formed">* Subject: * Body: * 6. Viết code cho sự kiện txtSubmit_Click GVHD: Dương Ngọc Long Nam – longnamit@yahoo.com Page 4
- 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 sự kiện txtSubmit_Click Viết code cho sự kiện txtSubmit_Click như 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) là text thông thường không phải code HTML. msg.IsBodyHtml = false; // Email, tên của người gởi lấy từ text box txtEmail và txtName. msg.From = new MailAddress(txtEmail.Text, txtName.Text); // Email của 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ì 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 và 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 các 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 5
- Building CMS / E-Commerce Project using ASP.NET 3.5 in C# 2008 and SQLServer 2005 7. Chạy và kiểm tra chương trình. 8. Thiết lập giao thức SMTP để send message, định nghĩa SMTP trong file web.config file như sau: ... ... 9. Cài đặt và cấu hình SMTP Service (nếu máy của bạn chưa có – Xem phụ lục kèm theo chương 3). 10. Tạo thư mục DAL trong thư mục App_Code/DAL 11. Tạo lớp DataAccess trong thư mục App_Code/DAL/DataAccess.cs. Và viết code như sau: namespace KimSoft.DAL { /// /// Lớp DataAccess là 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ả về Cache protected Cache Cache { GVHD: Dương Ngọc Long Nam – longnamit@yahoo.com Page 6
- 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 7
- 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ả về 1 danh sách đối tượng lưu trong 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ả về 1 danh sách đối tượng lưu trong IDataReader /// /// /// /// protected IDataReader ExecuteReader(DbCommand cmd, CommandBehavior behavior) { return cmd.ExecuteReader(behavior); } /// /// Thực thi câu lệnh select store trả về 1 giá trị /// /// /// object protected object ExecuteScalar(DbCommand cmd) { return cmd.ExecuteScalar(); } } } 12. Thêm vào 2 namespace sau cho Cach và DbCommand: using System.Web.Caching;//Cache using System.Data.Common;//DbCommand 13. Tạo thư mục BLL trong thư mục App_Code. 14. Tạo lớp BizObject.cs trong thư mục App_Data/BLL/ định nghĩa một số thuộc tính, phương thức thường xuyên được sử dụng bởi những đối tượng khác trong BLL như 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 8
- Building CMS / E-Commerce Project using ASP.NET 3.5 in C# 2008 and SQLServer 2005 // Trả về Cache protected static Cache Cache { get { return HttpContext.Current.Cache; } } //Lấy thông tin của User hiện tại. protected static IPrincipal CurrentUser { get { return HttpContext.Current.User; } } //Lấy tên của User hiện tại. protected static string CurrentUserName { get { string userName = ""; if (HttpContext.Current.User.Identity.IsAuthenticated) userName = HttpContext.Current.User.Identity.Name; return userName; } } //Lấy IP của User hiện tại. protected static string CurrentUserIP { get { return HttpContext.Current.Request.UserHostAddress; } } /// /// Trả về chỉ số trang /// /// /// /// int protected static int GetPageIndex(int startRowIndex, int maximumRows) { if (maximumRows
- Building CMS / E-Commerce Project using ASP.NET 3.5 in C# 2008 and SQLServer 2005 IDictionaryEnumerator enumerator = BizObject.Cache.GetEnumerator(); while (enumerator.MoveNext()) { if (enumerator.Key.ToString().ToLower().StartsWith(prefix)) itemsToRemove.Add(enumerator.Key.ToString()); } foreach (string itemToRemove in itemsToRemove) BizObject.Cache.Remove(itemToRemove); } } } 15. Thêm vào các namespace sau: using System.Web.Caching;//Cach using System.Security.Principal;//IPrincipal using System.Collections.Generic;//List using System.Collections;//IDictionaryEnumerator 16. Tạo thư mục Bin cho project. Right-click project, chọn Add ASP.NET Folder Bin. 17. Copy file MB.TheBeerHouse.CustomEvents.dll vào thư mục Bin của KimSoft. 18. Thêm đoạn code sau vào file web.config giữa để log lại các thao tác delete của Admin hay Editor: ...
- Building CMS / E-Commerce Project using ASP.NET 3.5 in C# 2008 and SQLServer 2005 provider="SqlWebEventProvider" profile="Critical" /> 19. Chạy và kiểm tra chương trình. ***HẾT CHƯƠNG 3*** GVHD: Dương Ngọc Long Nam – longnamit@yahoo.com Page 11

CÓ THỂ BẠN MUỐN DOWNLOAD
-
Building CMS E-Commerce Project using ASP.NET 3.5 in C# 2008 and SQLServer 2005 - Chương 2
25 p |
285 |
150
-
AVR GCC Tutorial (WinAVR)
111 p |
187 |
54
-
Microsoft C# Professional Projects
957 p |
122 |
31
-
Systems Analysis and Design
518 p |
114 |
30
-
CMS Design Using PHP and jQuery
340 p |
98 |
30
-
Arduino Programming with .NET and Sketch
177 p |
59 |
12
-
Building XML-Enabled Applications using Microsoft® SQL Server™ 2000
12 p |
99 |
10
-
Building iPhone Apps with HTML, CSS, and JavaScript - Making App Store Apps Without Objective-C or Cocoa
186 p |
47 |
7
-
Week 1: THE C# LANGUAGE
50 p |
60 |
7
-
Building Mobile Applications with Java
84 p |
47 |
6
-
SYSTEMS ANALYSIS AND DESIGN IN A CHANGING WORLD
0 p |
116 |
6
-
Programming Razor
118 p |
49 |
6
-
Module 3: Creating Forms by Using Microsoft Outlook 2000
94 p |
89 |
5
-
Building Collaborative Solutions by Using Microsoft® Outlook® 2000
14 p |
74 |
4
-
Become an XcoderStart Programming the Mac Using Objective-CBy Bert Altenberg, Alex Clarke and Philippe Mougin.LicenseCopyright noticeCopyright © 2008 by Bert Altenburg, Alex Clarke and Philippe Mougin. Version 1.15 Released under a Creative Commons
69 p |
27 |
3
-
DESIGN OF NETWORK SECURITY PROJECTS USING
12 p |
31 |
2
-
Automatic time-cost trade-off for construction projects using evolutionary algorithm integrated into a scheduling software program developed with .NET framework
6 p |
9 |
0


Chịu trách nhiệm nội dung:
Nguyễn Công Hà - Giám đốc Công ty TNHH TÀI LIỆU TRỰC TUYẾN VI NA
LIÊN HỆ
Địa chỉ: P402, 54A Nơ Trang Long, Phường 14, Q.Bình Thạnh, TP.HCM
Hotline:0933030098
Email: support@tailieu.vn
