This project is read-only.

Load View and Parse It. Is this possible?

Sep 15, 2012 at 1:26 AM

Hello,

I am loading a cshtml file and parse it as follows:

  String template = System.IO.File.ReadAllText(path);
  String output = RazorEngine.Razor.Parse(template, new { Name = "John" });

The cshtml is compiled using RazorGenerator.

Is there a way to load it instead of using File?

And is there a way to parse it using MVC engine instead of Razor Engine?

NOTE: The view is on a class library not on a MVC project.

Thank You,

Miguel

Sep 15, 2012 at 1:50 AM

Sounds like you're using RazorEngine, which is a separate project: http://razorengine.codeplex.com/

David

Sep 15, 2012 at 2:18 AM

Yes, but RazorEngine loads the file using its path.

But I am using RazorGenerator so I am trying to load the View and then parse it with MVC Engine or Razor Engine.

I tried the following:

var view = new VerifyUserView();

Where VerifyUserView is the following:

  [System.CodeDom.Compiler.GeneratedCodeAttribute("RazorGenerator", "1.4.1.0")]
    [System.Web.WebPages.PageVirtualPathAttribute("~/Templates/VerifyUserView.cshtml")]
    public class VerifyUserView : System.Web.Mvc.WebViewPage<dynamic>
    {
        public VerifyUserView()
        {
        }
        public override void Execute()
        {
            // Code
        }
}

But I don't know how to convert it to string ... I was looking for Execute but it does return nothing ...

How can I do this?

Thank You,

Miguel

Sep 15, 2012 at 2:30 AM

Sorry, I'm still getting this. Both RazorGenerator and RazorEngine support parsing cshtml files (though in different ways). So I don't get the scenario where you would end up using both together. They seem pretty exclusive to me.

Sep 15, 2012 at 2:37 AM
davidebbo wrote:

Sorry, I'm still getting this. Both RazorGenerator and RazorEngine support parsing cshtml files (though in different ways).

I didn't know that ... So consider only RazorGenerator:

On a class library I have a few Razor Mail Templates (cshtml files). Example:

@model dynamic

Hello @Model.Name,

Please come visit our site at ...

How can I, in a class method, load this file, fill the model and convert the result to String so I can send it by email?

Basically, this is what I have been trying to do ...

Is this possible only with Razor Generator?

Thank you,

Miguel

Sep 15, 2012 at 2:54 AM

I have been looking at the Execute method of my View generated by RazorGenerator and it is the following:

  public override void Execute() {
     WriteLiteral("\r\n\r\n");
     WriteLiteral("\r\n<p>Hello ");
     #line 5 "..\..\Templates\Verify.cshtml"
     Write(Model.Name);
     #line default
     #line hidden
     WriteLiteral(", please come visit our site at ...</p>\r\n\r\n");
  }

So basically this output is what I need to get replacing Model values by the Object properties I pass.

Then convert the entire output to string and use it in an email ...

Thank You,

Miguel

Sep 16, 2012 at 8:58 PM

Ok, now I understand your question, once we get RazorEngine out of the equation. Check out this post, which shows how to do this.

Sep 16, 2012 at 10:41 PM
davidebbo wrote:

Ok, now I understand your question, once we get RazorEngine out of the equation. Check out this post, which shows how to do this.

That is great ... You should publicly better your tools ... :-)

I often find some information but very dispersed and sometimes vague.

I found a "problem" with RazorGenerator Templating. I created a template in a class library and used it:

    VerifyUserEmail templ = new VerifyUserEmail {
      Model = new { Name = "Name" }
    };

    String result = templ.TransformText();  

It works fine. Then I moved this code to a Test project.

NOTE:  The template is still in the class library. The test project as a reference to class library.

When I run the test with this code I get the following error:

  'object' does not contain a definition for 'Name'

Any idea why? Maybe using dynamic is not allowed for this?

Maybe it should be a Strong Typed Model?

Thank You,

Miguel

Sep 17, 2012 at 12:51 AM

Yes, documentation in this project is a bit spotty :)

Is that a compile or runtime error? I'd need to see a more complete sample to see what's going on.

Sep 18, 2012 at 10:38 AM
Edited Sep 18, 2012 at 10:40 AM
davidebbo wrote:

Is that a compile or runtime error? I'd need to see a more complete sample to see what's going on.

It is a runtime error when I run the test ... I had this same error with another library.

I think dynamic objects are defined as internal. So to use a method that has a dynamic object as argument you need to:

1 - Use ExpandoObject on the original library:

dynamic expando = new ExpandoObject() 
{
  { "Name", "David" },
  { "Project", "RazorGenerator" }
};
return expando;

MSDN: http://msdn.microsoft.com/en-us/library/system.dynamic.expandoobject.aspx

Or maybe using an extension to convert from dynamic to ExpandoObject.

Or maybe using [assembly: InternalsVisibleTo("AssemblyName")] ... I think this case is not an option.

Anyway, at least I think the problem is detected.

Sep 18, 2012 at 5:27 PM

Yes, definitely something to do with expando, and not directly related to RazorGenerator. All RazorGenerator does is generate some code from your template, but after that, your code just runs with no RazorGenerator involvement. You can debug the generated code and see what's going on.