Using Razor engine on strings - not views

后端 未结 3 1618
囚心锁ツ
囚心锁ツ 2021-01-12 08:26

I\'d like to use the Razor engine without view (cshtml) files, but on strings. I want to do it from within MVC, I\'ve seen examples that use

new RazorViewE         


        
3条回答
  •  礼貌的吻别
    2021-01-12 08:54

    In order to use the RazorEngine to parse strings, you will need the RazorEngine.dll, which can be downloaded from http://razorengine.codeplex.com/

    To parse a string with the Razor engine just use the following example:

    var model = new { Name = "Test" };
    var template = "Hello @Model.Name";
    
    var result = Razor.Parse(template, model);
    

    As to whether or not it is advisable to use it for parsing a string, really depends on what you are using it for. If you think you are going to need the flexibility that Razor offers, I would recommend it, but it does come with a bit of a performance hit when comparing it to a standard string replace.

提交回复
热议问题