Razor.Parse alternative

2019-05-17 12:09发布

问题:

We have a default value for a textbox that is coming from a database and it is

@UserSession.NumberOfPeople

This is currently parsed by using

string value = Razor.Parse(Model.DefaultValue)

Which returns me the string value from that variable

However as Razor.Parse is now obsolete, I was looking into an alternative way using Engine.Razor.Compile but I can't seem to find an overload that isn't requiring a template

Is there a way of just parsing the the value as it is

string value = Razor.Parse("@UserSession.NumberOfPeople")

Or do I need to create some sort of template or install a nuget package for this?

回答1:

You could use RazorEngine and simply use your string as the template:

Engine.Razor.RunCompile(Model.DefaultValue, "templateKey", null, Model);

NOTE: For the RunCompile to accept a string as a template you need to use RazorEngine.Templating (@using RazorEngine.Templating in your view).