Hardik Patel

Hardik Patel

  • NA
  • 49
  • 895

How to fill razor engine template using Dictionary in c#?

Jul 19 2022 12:31 PM
public void checkFile()
{
   //Read file
   string SendData = System.IO.File.ReadAllText(@"Documents/requestTest.txt");

   //Prepare values in dictionary
   Dictionary<string, string> dict = new Dictionary<string, string>();
   dict.Add("FirstName", "Vishal");

   //Compile the razor template
   Engine.Razor.Compile(SendData, "TESTREQUEST");

   //fill the razor templates with dictionary values
   var data = Engine.Razor.Run("TESTREQUEST", null, dict);
}

Hi, I am using Asp.net core 3.1 in for my project. 

I am using razor engine template for generate API request. Razor engine template is created by third party users that's why there is no any common model for generate razor file with replace values. Also, we can not build individual model for each razor engine templates. 

For resolve the above issue we are using Dictionary for fill the razor engine template with values. But it's not working. 

I have spent few days on find the solution but no luck. 

So how can I pass dictionary instead of model into razor template and can replace values of emplates?