aspnet / Razor

[Archived] Parser and code generator for CSHTML files used in view pages for MVC web apps. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
883 stars 225 forks source link

RazorProjectEngine and using Razor in a Console app #2485

Closed genifycom closed 6 years ago

genifycom commented 6 years ago

I cannot seem to find anything on how to implement Razor in a C# Console App for .NET Core 2.1

Compilation indicates "'RazorEngine.Create(Action)' is obsolete" and "The recommended alternative is RazorProjectEngine.Create".

In addition previous 2.0 examples no longer work.

Please can we have a working sample of using the Razor engine using .NET Core 2.1 in a C# Console app.

mkArtakMSFT commented 6 years ago

Thanks for contacting us, @genifycom. @NTaylorMullen, do we have an updated sample somewhere, which we can point customers to?

NTaylorMullen commented 6 years ago

@genifycom the latest RazorPageGenerator is 2.1 (Razor without MVC).

Also our samples repo has a "render view to string" example using MVC.

mkArtakMSFT commented 6 years ago

Thanks for contacting us. We believe that the question you've raised have been answered. If you still feel a need to continue the discussion, feel free to reopen it and add your comments.

genifycom commented 6 years ago

The sample you indicated requires MVC but I am looking for a console app that does not require a webserver. A sample usage would be if you create an AWS Lambda Function (i.e. a single entry point with no routing). So one calls this one function and Razor returns the string generated from template.

If you include MVC, then you have to support the whole hosting environment which is not needed.

So is Razor supported without MVC?

NTaylorMullen commented 6 years ago

Parsing files in Razor without MVC is what my RazorPageGenerator link above accomplishes. If you're looking to render the contents of that page to a string, then you'll be responsible for providing the types referenced in the generated code that MVC usually provides.

Also to respond to

If you include MVC, then you have to support the whole hosting environment which is not needed.

You should be able to get most of the away with using the MVC types from Microsoft.AspNetCore.Mvc.Razor (doesn't depend on hosting).

genifycom commented 6 years ago

NTaylorMullen So I have it generating the designer files. But I can't seem to satisfy references in the generated files. For example:

Microsoft.Extensions.RazorViews.BaseView (looks like this changed).

I have Microsoft.AspNetCore.Mvc.Razor included.

Any tips?

NTaylorMullen commented 6 years ago

When using Roslyn to compile your Razor files I'd recommend using DependencyContext to capture your apps current set of references to allow for view compilation.

genifycom commented 6 years ago

Oh I need this for the compiled files right?

https://dotnet.myget.org/feed/aspnetcore-dev/package/nuget/Microsoft.Extensions.RazorViews.Sources

NTaylorMullen commented 6 years ago

You don't need to have them. You can create those types for yourself if you want.