Is there an example on using Razor to generate a s

2019-02-18 20:57发布

问题:

I want to generate a static HTML page by RAZOR, basically by using includes of partial sub pages.

  1. I have tried T4 as well and do look for an alternative: see here and here
  2. This answer says it is possible - but no concrete example
  3. I have installed Razor generator because I thought this is the way to go, but I do not get how to generate static HTML with this.

Best would be a complete extension which behaves like the T4 concept, but allows me to use the RAZOR syntax and HTML formatting (the formatting issue is basically the reasons why I am not using T4).

回答1:

If you are trying to take a Razor view and compile it and generate the HTML then you can use something like this.

public static string RenderViewToString(string viewPath, object model, ControllerContext context)
{
    var viewEngineResult = ViewEngines.Engines.FindView(context, viewPath, null);
    var view = viewEngineResult.View;


    context.Controller.ViewData.Model = model;

    string result = String.Empty;
    using (var sw = new StringWriter())
    {

        var ctx = new ViewContext(context, view,
                                  context.Controller.ViewData,
                                  context.Controller.TempData,
                                  sw);
        view.Render(ctx, sw);

        result = sw.ToString();
    }

    return result;
}

Or outside of ControllerContext http://razorengine.codeplex.com/



回答2:

The current version of Razor Generator has the "Generator" option which when used with the "MvcHelper" generator produces a static method for the helpers too.

For example, add this line at the top of your CSHTML file (with the Custom Tool Visual Studio property set to RazorGenerator of course):

@* Generator: MvcHelper, GeneratePrettyNames : true *@

The pretty names option is not strictly necessary but is something I feel should be default, to avoid those crazy long class names with underscores :-)

As you may know already, the main benefit of this method is you can share your helpers in separate assemblies. That is why I use Razor Generator in the first place.

Even within the same assembly, you could now leave your code outside App_Code folder. However that is not the best practice (at least for security) and the Visual Studio designer gets confused. It thinks the method is still not static, but it isn't and works fine.

I'm prototyping my helpers in the App_Code folder of the same site/assembly for speed then copying them to shared components when they're tested. The reason I needed this solution was to create generic Bootstrap helpers without hand-coding every piece of HTML in a HtmlHelper, i.e. used together with this solution from @chrismilleruk.

I guess later I may have to convert the CSHTML helpers to a hand-coded HtmlHelper for speed. But to start with see a great development speed increase at the beginning, from the ability to copy and paste blocks of HTML code I want to automate, then perfect and debug them quickly in the same format/editor.