直到后来发生了邮箱事件,我竟然忘了给邮箱密码赋值,导致遇到“邮箱不可用。 服务器响应为: 5.7.1 Unable to relay for”的问题,网上一查后,让Boss去设置IIS里的SMTP。
数据库以Xml字段存档多语言,格式为:
< ML V ="1.0" >
< M L ="zh-cn" >中文 </ M >
< M L ="en" >English </ M >
< M L =".." >其它语言 </ M >
</ ML >
SQL:查询语法为:
取值:字段名.value('(/ML/M[@L="zh-cn"])[1]','nvarchar(max)')
取节点:字段名.query('/ML/M[@L="en"]')
判断:字段名.exists('/ML/M[@L="zh-cn"]')
排序:用取值后的字段名进行排序
protected override void OnResultExecuted(ResultExecutedContext filterContext)
{
if (filterContext.Result is ViewResult)
{
string html = RenderViewToString( this, ((ViewResult)filterContext.Result).View);
html = LanguageMgr.Replace(html, " zh ");
Response.Clear();
Response.Write(html);
}
}
protected static string RenderViewToString(Controller controller, IView view)
{
// IView view = ViewEngines.Engines.FindView(controller.ControllerContext, viewName, masterName).View;
using (System.IO.StringWriter writer = new System.IO.StringWriter())
{
ViewContext viewContext = new ViewContext(controller.ControllerContext, view, controller.ViewData, controller.TempData, writer);
viewContext.View.Render(viewContext, writer);
return writer.ToString();
}
}
public class LanguageMgr
{
/// <summary>
/// 替换多语言。
/// </summary>
/// <param name="html"></param>
/// <param name="lang"></param>
/// <returns></returns>
public static string Replace( string html, string lang)
{
MatchCollection matchs = Regex.Matches(html, @" \[#([\S\s]*?)#\] ", RegexOptions.Compiled | RegexOptions.IgnoreCase);
if (matchs != null && matchs.Count > 0)
{
List< string> keys = new List< string>(matchs.Count); // 记录已匹配过的
Dictionary< string, string> dic = GetLanguageDic(lang);
foreach (Match match in matchs)
{
string text = match.Groups[ 0].Value;
string key = match.Groups[ 1].Value.Trim();
if (!keys.Contains(key))
{
keys.Add(key);
if (dic.ContainsKey(key))
{
html = html.Replace(text, dic[key]);
}
}
}
keys = null;
matchs = null;
}
return html;
}
internal static Dictionary< string, string> GetLanguageDic( string lang)
{
Dictionary< string, string> dic = new Dictionary< string, string>();
dic.Add( " aaa ", " 中文 ");
dic.Add( " bbb ", " 英文 ");
return dic;
}
}