How to parse a Razor template with particles from a user folder?

I have two cshtml files in the same Views subfolder. One of the templates is designed to include another template. I tried to do it as follows:

The main template:

<html> <head></head> <body> @Html.Partial("~/Views/Pdfs/Header"); </body> </html> 

The error I get is

Unable to compile template. The name "Html" does not exist in the current context.

What should I do extra?

+5
source share
1 answer

As Eric commented, there is no Html in RazorEngine (see related answer), however you can use @Include("mytemplate") .

If you want to be compatible with the @Html.Partial() syntax for any reason, you can extend the RazorEngine syntax like this .

Basically what you want to do is to provide your own class that inherits from TemplateBase<T> (or ITemplate , to be precise), and then set it either using the configuration or using the @Inherit MyBaseClass<MyModel> syntax. In this case, you can simply call the Include method from your Partial method in the Html helper class.

+3
source

Source: https://habr.com/ru/post/1205461/


All Articles