用数据库中的值替换字符串中的代码

时间:2013-01-01 09:14:18

标签: c# asp.net

我正在努力替换字符串中的一些代码。我有一个函数可以从emailtemplates表中获取一个电子邮件模板,其中包含##NewUserFirstName####password##等代码  在该表的正文列中。还有另一个表定义了哪个表将替换这些代码的值,因此##NewUserFirstName##我在第二个表中定义了UserProfile.FirstName##password## UserProfile.password。我有这样的所有合并代码:

  using (IMailMergeCodeManager mailMergeCodeManager = new ManagerFactory().GetMailMergeCodeManager())
    {
        mailMergeCodes = mailMergeCodeManager.GetAll().ToList();
    }

现在我用这些代码替换模板中的代码:

 using (ICampaignManager campaignManager = new ManagerFactory().GetCampaignManager())
 {
     using (IUserProfileManager UserProfileManager1 = new ManagerFactory().GetUserProfileManager())
     {
         using (IAccountManager AccountManager = new ManagerFactory().GetAccountManager())
         {
             using (ICallTypeManager CallTypeManager = new ManagerFactory().GetCallTypeManager())
             {
                 using (ICallOutcomeManager CallOutComeManager = new ManagerFactory().GetCallOutcomeManager())
                 {
                     using (ICallRecordingManager CallRecordingManager = new ManagerFactory().GetCallRecordingManager())
                     {
                         UserProfile UserProfile = UserProfileManager1.GetByfkAccountId(Convert.ToInt32(AccountId)).FirstOrDefault();
                         Account acc = AccountManager.GetById(Convert.ToInt32(AccountId));                                      
                         UserProfile CurrentLoggedInUser = UserProfileManager1.GetByfkAccountId(Convert.ToInt32(CurrentAccount.Id)).FirstOrDefault();
                         CallRecording callRecording = CallRecordingManager.GetById(callRecordingId);

                         if (subject.Contains("##password##") || body.Contains("##password##"))
                             password = GetPassword(UserProfile.UserAuthentication.EmailAddress);


                         foreach (MailMergeCode mergeCode in mailMergeCodes)
                         {
                             if (body.Contains(mergeCode.MailMerge)) 
                                 body = Regex.Replace(body, mergeCode.MailMerge, mergeCode.FieldName, RegexOptions.IgnoreCase);


                             if (subject.Contains(mergeCode.MailMerge) && !subject.Contains("##password##"))
                                 subject = Regex.Replace(subject, mergeCode.MailMerge, mergeCode.FieldName, RegexOptions.IgnoreCase);

                         }

                     }
                 }
             }
         }
     }

 }

}

但是,后续行将##NewUserFirstName##设为UserProfile.FirstName

body = Regex.Replace(body, mergeCode.MailMerge, mergeCode.FieldName, RegexOptions.IgnoreCase);

而不是从UserProfile.FirstName获取“Joe Smith”

的价值

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

使用此代码,如果您有任何问题,请告诉我

if (Body.Contains(""##password##""))
 {
 Body = Body.Replace(""##password##"", yourpassword_string);
 }