Question

There are a couple of "requirements":

1) The application must generate static HTML files from razor markup.

2) The controller in achieving (1), needs to use views and models that are already defined in application.

I understand that RazorEngine can combine a trivial template and a model as a string containing Html:

string template = "Hello @Model.Name! Welcome to Razor!";   
string result = Razor.Parse(template, new { Name = "World" });

The documentation doesn't say much but the issue here is that this doesn't go far enough. I want the Parse method to either accept:

a) the name of a view known to the controller or

b) the path of the view.

Is there a way to use an actual MVC view and model to create an Html file? A working example would be appreciated.

Was it helpful?

Solution

b) the path of the view.

If you know the path of the view, you could read its contents:

string viewPath = "...";
string template = File.ReadAllText(viewPath);

and then as the documentation states you could use the Razor Engine to pass a model and the contents of the view to get the markup:

string result = Razor.Parse(template, new { Name = "World" });
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top