public override void OnExecute(ICommandContext context, CommandConfig config, CommandResult result)
        {
            var     viewResult = result as ActionCommandResult;
            dynamic viewModel  = null;

            if (RazorTemplate.IsIgnoreModelType)
            {
                viewModel = new { Context = context, Parameters = Parameters.CreateDictionary(context) };
            }
            else
            {
                viewModel = new RazorContentViewModel {
                    Context = context, Parameters = Parameters.CreateDictionary(context)
                };
            }

            try
            {
                if (string.IsNullOrEmpty(TemplateFile))
                {
                    throw LangTexts.Current.GetLangText("9013", "模板没有设置").CreateException();
                }

                var template = ContentLib.Current.FolderRoot.GetFile(TemplateFile);
                if (template == null || (!(template is RazorTemplate)))
                {
                    throw LangTexts.Current.GetFormatLangText("9011", "模板[{0}]加载失败", TemplateFile).CreateException();
                }
                var razorTemplate = template as RazorTemplate;

                var           setting = UISettings.Current.Items[SettingType] as UISetting;
                StringBuilder sb      = new StringBuilder();
                if (setting != null)
                {
                    sb.Append(setting.ContentHeader);
                }

#if DEBUG
                DateTime beginRender = DateTime.Now;
#endif
                sb.Append(razorTemplate.Render(viewModel));
#if DEBUG
                this.Log(string.Format("Razor Render\t{0}-{1}:{2}", beginRender.ToString("HH:mm:ss.fff"),
                                       DateTime.Now.ToString("HH:mm:ss.fff"), (DateTime.Now - beginRender).TotalSeconds));
#endif
                if (setting != null)
                {
                    sb.Append(setting.ContentFooter);
                }
                viewResult.Content = sb.ToString();
            }
            catch (Exception ex)
            {
                viewResult.Content = ex.Message;
            }
            viewResult.ContentType = ContentType;
            viewResult.Encoding    = Encoding;

            result.End(NextCommand);
            context.Execute(NextCommand);
        }
Exemple #2
0
        public override void OnExecute(ICommandContext context, CommandConfig config, CommandResult result)
        {
            var viewResult = result as MailResult;
            var viewModel  = new RazorContentViewModel {
                Parameters = Parameters.CreateDictionary(context)
            };

            try
            {
                if (string.IsNullOrEmpty(TemplatePath))
                {
                    throw LangTexts.Current.GetLangText("9013", "模板没有设置").CreateException();
                }

                if (TemplatePath.Contains("{@"))
                {
                    viewResult.Content = TemplatePath.ReplaceContextParameters(context, viewModel);
                }
                else
                {
                    var template = ContentLib.Current.FolderRoot.GetFile(TemplatePath);
                    if (template == null || (!(template is RazorTemplate)))
                    {
                        throw LangTexts.Current.GetFormatLangText("9011", "模板[{0}]加载失败", TemplatePath).CreateException();
                    }
                    var razorTemplate = template as RazorTemplate;
                    viewResult.Content = razorTemplate.Render(viewModel);
                }


                var title  = Title.ReplaceContextParameters(context, viewModel);
                var sendTo = SendTo.ReplaceContextParameters(context, viewModel);
                var server = GetMailServer();
                if (string.IsNullOrEmpty(sendTo))
                {
                    throw new Exception("邮件接收者不能为空");
                }

                viewResult.Title    = title;
                viewResult.SendTo   = sendTo;
                viewResult.Encoding = Encoding;

                //在Action配置中增加可否为订阅事件的标识,如果有,则在该Action中增加一个MailAction,在该成功执行之后再执行
                if (sendTo.IndexOf(',') > 0 && SendOneByOne)
                {
                    foreach (var to in sendTo.Split(','))
                    {
                        server.SendMail(title, viewResult.Content, to);
                    }
                }
                else if (!string.IsNullOrEmpty(sendTo))
                {
                    server.SendMail(title, viewResult.Content, sendTo);
                }
            }
            catch
            {
                throw;
            }

            result.End(NextCommand);
            context.Execute(NextCommand);
        }