集成选项对应的是Shop.Operaional类库,增加这个类库将增强系统的可伸缩性,简化调试,改进课维护性并且提供在其他项目中重用代码的能力。
首先实现电子邮件管理程序
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Configuration;
namespace Shop.Operational
{
/// <summary>
/// 发送邮件处理类
/// </summary>
public class EmailManager
{
public EmailManager()
{
}
public bool IsSent { set; get; }
//读取存储在web.config文件中的数据
private string SMTPServerName
{
get { return ConfigurationManager.AppSettings["SMTPServer"]; }
}
private string ToAddress
{
get { return ConfigurationManager.AppSettings["ToAddress"]; }
}
/// <summary>
/// 发送方法
/// </summary>
/// <param name="emailcontents">邮件内容结构体</param>
public void Send(EmailContents emailcontents)
{
//发送电子邮件类
SmtpClient client = new SmtpClient(this.SMTPServerName);
client.UseDefaultCredentials
= true;
//发件人地址和姓名
MailAddress from = new MailAddress(emailcontents.FromEmailAddress, emailcontents.FromName);
//收件人的地址
MailAddress to = new MailAddress(this.ToAddress);
MailMessage message
= new MailMessage(from, to);
message.Subject = emailcontents.Subject;
message.Body = Utilities.FormatText(emailcontents.Body, true);
message.IsBodyHtml = true;
try
{
//发送邮件
client.Send(message);
IsSent = true;
}
catch (Exception ex)
{
throw ex;
}
}
}
/// <summary>
/// 邮件内容结构体
/// </summary>
public struct EmailContents
{
public string To;
public string FromName;
public string FromEmailAddress;
public string Subject;
public string Body;
}
}
这里只是提醒一下SmtpClient和MailAddress是在System.Net.Mail命名空间下面。SMTPServerName就是服务器名称,ToAddress就是要发送到邮件的地址,他们在web.config文件的存储形式如下:
<appSettings>
<add key="SMTPServerName" value="localhost"/>
<add key="ToAddress" value="couhujia@sina.com"/>
</appSettings>
实现游客发送邮件的话就构造EmailManager类型对象的一个实例,然后赋值给这个对象的属性,在调用send发送方法就ok了。在注意一下Body属性使用了Utilities的类和名为FormatText静态方法。
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
namespace Shop.Operational
{
public class Utilities
{
public static string FormatText(string text, bool allow)
{
string formatted = "";
StringBuilder sb
= new StringBuilder(text);
sb.Replace(" ", " ");
if (!allow)
{
sb.Replace("<br>", Environment.NewLine);
sb.Replace(" ", " ");
formatted = sb.ToString();
}
else
{
StringReader sr = new StringReader(sb.ToString());
StringWriter sw = new StringWriter();
//能否读取下一个字符
while (sr.Peek() > -1)
{
string temp = sr.ReadLine();
sw.Write(temp + "<br>");
}
formatted
= sw.GetStringBuilder().ToString();
}
return formatted;
}
}
}
因为假设这里不考虑用组件而是文本框,那么输入的字符就需要将其转化为HTML格式正确的表示。allow设置为true时就是将输入到文本框里的字符串格式转化成了HTML格式。allow设置为false时就是将HTML格式转化为字符串格式.
这里想说明一下System.Environment类型定义了一个只读的NewLine属性。如果程序在Windows上运行,该属性能返回一个由这些字符构成的字符串。NewLine属性是依赖与平台的,它会根据底层平台来返回恰当的字符串。例如,将公共语言基础结构(CLI)移植到一个UNIX系统,NewLine属性会返回一只包含字符\n的字符串。所以就建议以后换行字符串用System.Environment.NewLine
使程序能够执行一些异常处理
首先这方面是我以前不太注意的地方,但是渐渐的发现这块功能其实很重要。因为无论什么程序,可能都会出现一些我们无法预料的错误或异常。我们就要使程序能够处理这些错误或异常
我们首先记录这些错误或异常记录到/ExceptionLog/LogFile.txt文件中,其实这里大家可以自己动手改改当发生异常或错误时候用邮件通知开发者。
Code
<%@ Application Language="C#" %>
<%@ Import Namespace="Shop.Operational" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
Exception ex = Server.GetLastError();
Utilities.LogException(ex);
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
</script>
注意到调用了了Shop.Operational命名空间下的Utilities类的LogExcepion()方法
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web;
namespace Shop.Operational
{
public class Utilities
{
public static string FormatText(string text, bool allow)
{
string formatted = "";
StringBuilder sb
= new StringBuilder(text);
sb.Replace(" ", " ");
if (!allow)
{
sb.Replace("<br>", Environment.NewLine);
sb.Replace(" ", " ");
formatted = sb.ToString();
}
else
{
StringReader sr = new StringReader(sb.ToString());
StringWriter sw = new StringWriter();
//能否读取下一个字符
while (sr.Peek() > -1)
{
string temp = sr.ReadLine();
sw.Write(temp + "<br>");
}
formatted
= sw.GetStringBuilder().ToString();
}
return formatted;
}
public static void LogException(Exception ex)
{
string path = HttpContext.Current.Server.MapPath(@"ExceptionLog\LogFile.txt");
using (StreamWriter sw = new StreamWriter(path, true))
{
sw.WriteLine(DateTime.Now + Environment.NewLine + ex.InnerException.ToString() + Environment.NewLine + Environment.NewLine);
}
}
}
}
这里有个一个地方不太明白怎么用using(....),不知一般用于引用命名空间吗?希望明白的朋友给我讲解一下
但是这只是给我们开发人员提供了通知,我们还要给游客提供通知说明程序出现错误
首先设置Web.config文件(当然你也可以为单独一张页面出现错误制作一个错误提示,那就要在Page属性里定义错误页面了,我这是所有的页面出现错误都跳转ErrorPage.aspx页面)
<customErrors mode="On" defaultRedirect="ErrorPage.aspx">
</customErrors>
可以在ErrorPage.aspx页面说一些 ”非常对不起对您造成了不便,但是网站遇到了错误,技术支持小组正在修复当中"等一些话语
另外大家也知道国外买商品,需要自己另外付购物的税钱,就是购物的总价钱*税率
Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace Shop.Operational
{
public class CalculationManager
{
public static decimal CalcSalesTax(decimal amount)
{
decimal total;
decimal taxrate = (Convert.ToDecimal(ConfigurationManager.AppSettings["TaxRate"]) / 100);
total = (amount * taxrate);
return total;
}
}
}
<
appSettings>
<add key="TaxRate" value="7"/>
<add key="SMTPServerName" value="localhost"/>
<add key="ToAddress" value="couhujia@sina.com"/>
</appSettings>
总之可以看见在这个类库里的代码都是一些日常常用的类。
大家可以谈谈自己在这方面的处理,让我们分享一下您的编程设计和思想。谢谢