Closed Cafnio closed 2 years ago
Hi, the best way is to parse json outside of template and pass it as ready to use dynamic. If it is not possible, this is how you make it:
you need to go this way:
IRazorEngine razorEngine = new RazorEngine();
IRazorEngineCompiledTemplate template = razorEngine.Compile(Content, builder =>
{
builder.AddAssemblyReference(typeof(JsonConvert));
builder.AddUsing("Newtonsoft.Json");
});
string result = template.Run(new
{
Json = "{a: 1}"
});
template:
@{
dynamic json = JsonConvert.DeserializeObject(Model.Json);
}
<div>json: @json.a</div>
well, this also works for me
IRazorEngine razorEngine = new RazorEngine();
IRazorEngineCompiledTemplate template = razorEngine.Compile(Content, builder =>
{
builder.AddAssemblyReference(typeof(JsonConvert));
});
string result = template.Run(new
{
Json = "{a: 1}"
});
@using Newtonsoft.Json;
@{
dynamic json = JsonConvert.DeserializeObject(Model.Json);
}
<div>json: @json.a</div>
Yep, got it working properly! Was adding the wrong AssemblyReference!
Thanks for the help :)
Hi there! First thanks for this awesome work!
I got all the Email Service implemented and working properly on other type of emails.
Yet, on a new type of email, the model contains a JSON string which I want to Deserialize and use the fields from it. Example:
On top of the .cshtml file, I have the following:
then on the code itself.
Yet, I recieve the error: "Could not load file or assembly 'Newtonsoft, Culture=neutral, PublicKeyToken=null'. System could not find the file"
I've tried the following, yet, the error persists:
Any idea to solve this issue?