2013-06-04 39 views

回答

1

在根\ App_Code文件夾只是創建例如MyRuleTemplate.cs類文件

namespace Ektron.Com.Helpers 
{ 
    public class MyRuleTemplate : RuleTemplate 
    { 

     private StringOperatorField _fieldOperator = null; 

     public MyRuleTemplate() 
      : base("My First RuleTemplate") 
     { 
     _fieldOperator = new StringOperatorField("operator", "operator", ""); 
     }   
     /// <summary> 
     /// Evaluating the rule template condition set in the widget 
     /// </summary> 
     /// <param name="rule"></param> 
     /// <returns></returns> 
     public override bool Evaluate(Rule rule) 
     { 
      //write the code for evaluate 
     string op = rule.Values["operator"]; 
      string visitorCountry = string.Empty; 
      string ruleCountry = rule.Values["name"]; 

      try 
      { 
       string IP = HttpContext.Current.Request["remote_addr"]; 
       if (!string.IsNullOrEmpty(HttpContext.Current.Request["ip"])) 
        IP = HttpContext.Current.Request["ip"]; 
       else if (!string.IsNullOrEmpty(HttpContext.Current.Session["ipaddress"].ToString())) 
        IP = HttpContext.Current.Session["ipaddress"].ToString(); 
       var userData = Ektron.Cms.UserContext.GetLocationInfo(IP); 
       visitorCountry = userData.CountryCode; 
      } 
      catch (Exception ex) 
      { 
       Ektron.Cms.EkException.LogException(ex, System.Diagnostics.EventLogEntryType.Error); 
      } 

      return _fieldOperator.Evaluate(visitorCountry, op, ruleCountry); 
     } 

    private Dictionary<string, Field> _fields = null; 
     public override Dictionary<string, Field> Fields 
     { 
      get 
      { 
       if (_fields == null) 
       { 
        _fields = new Dictionary<string, Field>(); 
        this.AddField(_fieldOperator); 
        List<LocalizableValue> countryFields = new List<LocalizableValue>(); 
        countryFields.Add(new LocalizableValue("US", "United States")); 
        countryFields.Add(new LocalizableValue("DE", "Germany")); 
        countryFields.Add(new LocalizableValue("CA", "Canada")); 
        countryFields.Add(new LocalizableValue("FR", "France")); 
        _fields.Add("name", new EnumField(new LocalizableValue("name", "name", ""), countryFields)); 
       } 
       return _fields; 
      } 
     } 


     /// <summary> 
     /// Text which will be displayed in the condition 
     /// </summary> 
     public override string Text 
     { 
      get 
      { 
       return "Custom Country {operator} {name}"; 
      } 
     } 
     /// <summary> 
     /// Title of the rule template 
     /// </summary> 
     public override string Title 
     { 
      get 
      { 
       return "Custom Country"; 
      } 
     } 


    } 
} 

然後,在\部件\ TargetedContent.ascx.cs方法AddAllRuleTemplates()內只需添加一個參考到新添加自定義規則模板MyRuleTemplate作爲AddRuleTemplate(new MyRuleTemplate());