2016-08-04 1 views
-1

に定数クラスのうちの辞書を作成します。私は様々なプレースホルダーを含むHTML文書を持っています。私のウェブAPIコントローラ(C#の)で、私は一度にこれらのプレースホルダ1を交換しています:は、私はいくつかの文字列定数を含むクラスを持つC#の

string doc = string.Empty; 
string htmltest = System.IO.File.ReadAllText  
(HttpContext.Current.Server.MapPath(@"~\Templates\Template.html")); 
doc = htmltest.Replace(TemplateConstants.ApplicationType, applicationType); 
     doc = doc.Replace(TemplateConstants.LoanPurpose, loanType); 
     doc = doc.Replace(ApplicationDocTemplateConstants.BorrowerFirstName, FirstName); 
..... 

何とかファイルを通じて代わりに、ここで私ができるように、ループを辞書を使用してに基づいて置換を行う方法はあります辞書(各置換を一度に1つずつ行う必要はありません)?

programicallyこの例のように
+0

これは、C#での文字列定数の処理方法ではありません。「static readonly」を「const」に置き換える必要があります。 readonly文字列はconst文字列と同じではありません**違いは、readonlyは実行時定数であり、constはコンパイル時のconstなので、constを使用するとコンパイラは静的に置換を実行できます。 – EJoshuaS

+1

nakisa、可能であれば、辞書の解決に行きません。難しい作業をするfrmaeworkを試してみてください。 下記のRazorの回答をご覧ください。それはあなたに多くの時間を節約するでしょう。 –

+1

私はAmirに同意します。あなたがしようとしているのはHTMLテンプレートです。その特定の目的のために書かれたライブラリ全体があります。彼らはあなたがまだ考慮していない問題を解決します(悪意のあるHTMLやJavaScriptをアプリケーションのファーストネームフィールドに挿入するとどうなりますか?) – StriplingWarrior

答えて

-1

使用カミソリ・ビュー・エンジン: http://razorengine.codeplex.com/ (nugetのRazorEngine)テンプレートは、カミソリの構文 ようにする必要がありますもちろんの

using RazorEngine; 
using RazorEngine.Templating; // For extension methods. 

class Program 
{ 
    static void Main(string[] args) 
    { 
     string template = "Hello @Model.Types, welcome to RazorEngine!"; 
     var result = 
      Engine.Razor.RunCompile(template, "templateKey", null, new TemplateConstants()); 

     Console.WriteLine(result); 
    } 
} 

public class TemplateConstants 
{ 
    public readonly string Types = "Some type"; 
    public readonly string Purpose = "«Purpose»"; 
    public readonly string FirstName = "«FirstName»"; 

} 

<html> 
... 
<div>@Model.Purpose</div> 
<div>@Model.FirstName</div> 
... 
</html> 

も参照してください。 https://github.com/Antaris/RazorEngine

-1

私は静的なものを変換しました。定数に変換し、定数を辞書に変換するメソッドを追加しました。

public class TemplateConstants 
{ 
    public const string Type = "«Type»"; 
    public const string Purpose = "«Purpose»"; 
    public const string FirstName = "«FirstName»"; 

    private static readonly Lazy<Dictionary<string, string>> ConstantsDictionary = 
     new Lazy<Dictionary<string, string>>(CreateDictionary); 

    public static Dictionary<string, string> AsDictionary() 
    { 
     return ConstantsDictionary.Value; 
    } 

    private static Dictionary<string, string> CreateDictionary() 
    { 
     var fields = typeof(TemplateConstants) 
      .GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy); 

     var constants = fields.Where(f => f.IsLiteral && !f.IsInitOnly).ToList(); 
     var result = new Dictionary<string, string>(); 
     var instance = new TemplateConstants(); 

     // Up to you if you want to filter the constants further to only include those 
     // with string values. 
     constants.ForEach(constant => result.Add(constant.Name, constant.GetValue(instance).ToString())); 
     return result; 
    } 
} 

[TestClass] 
public class TestTemplateConstants 
{ 
    [TestMethod] 
    public void TestDictionary() 
    { 
     var dictionary = TemplateConstants.AsDictionary(); 
     Assert.AreEqual(dictionary["Type"],TemplateConstants.Type); 
    } 
} 
+2

BTW - 私は辞書を使用することはおそらくあなたがやろうとしていることをする方法ではないという他のコメントに同意します。文字通り質問に答えるだけです。これを行うと便利なシナリオがあります。 –

関連する問題