Search code examples
c#razorengine

Convert razor syntax to plain html using razorEngine


I am trying to convert razor view syntax to plain HTML using a library called RazorEngine But when I run the below code, it gives me localhost is currently unable to handle this request. HTTP ERROR 500 during the line of var result.

What am I doing wrong?

Controller code:

string template = @"Hi @Model.Name";

            var model = new UserModel() { Name = "Sarah" };

            var result = Engine.Razor.RunCompile(template, "templateKey", null, model);

Model:

public class UserModel
        {
            public string Name { get; set; }
        }

Solution

  • If you look at the quickstart, passing null into:

    var result = Engine.Razor.RunCompile(template, "templateKey", null, model);
    

    means you are using a dynamic model.

    Since you are not using a dynamic model, you need to specify the model type:

    var result = Engine.Razor.RunCompile(template, "templateKey", typeof(UserModel), model);