Is there an example of using Razor to create a static HTML page?

I want to create a static HTML page using RAZOR, mainly using partial sub pages.

  • I tried T4 too and am looking for an alternative: see here and here
  • This answer says it is possible - but a concrete example
  • I installed the Razor generator because I thought this was the way to go, but I don't understand how to create static HTML with this.

The best would be a full extension, which behaves like the concept of T4, but allows you to use the RAZOR syntax and HTML formatting (the formatting problem is mainly the reason why I do not use T4).

+3
html visual-studio-2010 razor razorgenerator
Sep 11 '12 at 10:25
source share
2 answers

If you are trying to make a Razor view and compile it and generate HTML, 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/

+3
Sep 11 '12 at 23:22
source share

The current version of the Razor Generator has a Generator parameter, which when used with the MvcHelper generator also creates a static method for helpers.

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

 @* Generator: MvcHelper, GeneratePrettyNames : true *@ 

A nice version of the names is not strictly necessary, but I think it should be the default to avoid these crazy long class names with underscores :-)

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

Even inside the same assembly, you can leave your code outside the App_Code folder. However, this is not the best practice (at least for security), and the Visual Studio designer is confused. He believes that the method is still not static, but it does not work and works fine.

I prototype my helpers in the App_Code folder of the same site / assembly for speed, and then copy them to common components when they are tested. The reason I needed this solution was to create universal Bootstrap helpers without manually encoding each HTML fragment in an HtmlHelper, i.e. used with this solution by @chrismilleruk.

I believe that later, I may have to convert the CSHTML helpers to HtmlHelper manual encoding for speed. But for starters, with a large increase in development speed at the beginning, with the ability to copy and paste blocks of HTML code, I want to automate, and then improve and quickly debug them in the same format / editor.

+1
Jul 24 '15 at 11:51
source share



All Articles