-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Documentation update to improve usage experience: @Inherits directive #13
Comments
Thank you for the kind words. Im surprised RazorEngine dont mind to see several https://github.com/adoconnection/RazorEngineCore/wiki/Switch-from-RazorEngine-cshtml-templates |
I have attached sample ConsoleApp48 that runs on NET5
using RazorEngineCore;
using System;
using System.IO;
namespace ConsoleApp48
{
class Program
{
static void Main(string[] args)
{
RazorEngine razorEngine = new RazorEngine();
string templateText = File.ReadAllText("Template.cshtml");
IRazorEngineCompiledTemplate<RazorEngineTemplateBase<EmailModel>> compiledTemplate = razorEngine.Compile<RazorEngineTemplateBase<EmailModel>>(templateText);
string result = compiledTemplate.Run(instance => {
instance.Model = new EmailModel
{
Email = "mail@example.com",
FirstName = "Jack"
};
});
Console.WriteLine(result);
Console.ReadKey();
}
}
} namespace ConsoleApp48
{
public class EmailModel
{
public string Email { get; set; }
public string FirstName { get; set; }
}
} @inherits RazorEngineCore.RazorEngineTemplateBase<ConsoleApp48.EmailModel>
<html>
<body>
<h1>Hello @Model.FirstName</h1>
</body>
</html> |
thanks for taking a look. i ran your stuff locally and it does work perfectly. the code you have for the template and compiling is nearly exactly the same as what we have. main difference i see is this is a console app whereas we have ASP.NET core MVC app. i spun up an mvc app that tries to compile a single template with the @inherits directive and return an OK result with the resulting string. it blows up in the same way as our actual application (which is during the build with the same errors i posted above) if you clone this repo and try to build it, you should see the same again thanks for looking, any advice is greatly appreciated |
The most likely scenario is that the A simple workaround is to rename the file extension or change the way the file is handled in the |
thanks @wdcossey. |
@jackhamby something like the following.
|
Default code:
I updated the |
thanks for all this. i played around with those snippets you sent but kept having the same issue. this weekend i finally got something to work. i changed the BuildAction property on the .cshtml file itself, from Content to EmbeddedResource. once i got that switched over i could build and run. compilation works great as well as the intellisense provided by @inherits directive. thanks again for the help in this. |
@jackhamby Man, you saved me. That's what I needed. Thank you very much! |
Firstly thank you for this project. I have battled with various other packages and couldn't get them to work reliably with .NET Core 3.1 running in an Azure Function. Yours works perfectly.
Here's something I feel would improve the user experience if it was documented...
For anyone who is storing their templates in CSHTML files either in the file system or as an embedded resource, the lack of the @model and @using statements mean you can't benefit from Visual Studio's IntelliSense when editing the files. You end up with lots of warnings in the code whereever you reference @Model.Property, or @include (if you are implementing the @include support from your wiki).
I have found that when using a strongly typed model, adding an @inherits directive to the file greatly improves this.
@inherits RazorEngineCore.RazorEngineTemplateBase<MyModel>
Or in my case where I'm using a custom template base to implement the include functionality, I'm using:
@inherits FunctionsApp1.Email.CustomRazorEngineTemplateBase<MyClassLib.EmailModels.WelcomeModel>
I hope this helps.
The text was updated successfully, but these errors were encountered: