Description
Is your feature request related to a problem? Please describe.
Hello dear developers.
Recently I had a need to remake the html emails so that RazorViewToStringRenderer
is used to create them. The specificity of html emails is such that there is one general layout and many repeating pieces (for example, buttons, tables, links with the same style, etc.).
At first I decided to use PartialView. However, there can easily be 10-20 PartialViews inside the letter.
And the performance of such solution turned out to be only two to three times faster than the previous implementation (ashamed to say, but used StringBuilder and Replace).
I thought that I should abandon the Partial View and came to the conclusion that it would be ideal to use the block
@functions
inside * .cshtml and put content from Partial Views there.
And this, as expected, turned out to be two to three times faster compared to the implementation based on PartialViews.
But there is a problem. The content from the @functions
block cannot be inherited (please correct me if I'm wrong).
Therefore, now I need to either copy the @function
s block in each letter (duplication is not good) or use PartialViews (but this is much slower).
Describe the solution you'd like
Can we make it possible to have access to the @functions block declared in a different layout?
I see two possible options
Option 1
We create a special attribute (for example, ShareFunctionsBlockAttribute). It has two parameters: the name of the output class and the namespace. If this attribute is specified in * .cshtml, then when compiling * .cshtml:
- a class with the name specified in the attribute is generated and placed in the specified namespace
- the modifier partial and abstract is put on the generated class
- the generated class is made generic
- the ExecuteAsync function is removed.
This will allow:
- Manually Create a generic abstract partial class with the same name specified in the attribute, declare the necessary partial functions, but implement them inside the generated class (which based on * .cshtml)
- Inherit all the necessary View from the class from the previous paragraph.
- Inside these Views, we can use the functions from the
@functions
block
Option 2
Give View access to the @functions
block declared in the file referenced by the Layout property (for example, in _Layout.cshtml).
But it is difficult for me to think over the implementation details. Could be by automatically inheriting from _Layout .cshtml (or some other file specified by the Layout property).
Conclusion
As a result, we will get a faster alternative to Partial Views. I'm not specifically talking about replacement, because there are certainly cases where Partial Views will come in handy.
Dear developers, what do you think about this proposal?
ps Everything described was tested on .net 6
pss At the moment I am using Source Generator which does it for me. It does not use a partial class, but makes an implementation of the interface that I created by hand. But there is a drawback - it only supports simple cases.