-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Lightweight and fast alternative to Partial View #38551
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
Comments
@sergei66666 thanks for contacting us. We think that using components is a better option than partial views for these scenarios and we have a workitem tracking enabling this. I'm going to close this issue and suggest you upvote the other issue instead for us to keep track of it. #38114 |
@javiercn Sorry to disturb you. I examined the link you attached. And I don't fully understand how this can help. I will give a more detailed example to describe the situation. We have a console application (web job) that sends html letters.
Therefore, it seems to me that it is very convenient to use the MVC approach here. Then, for example, we create The problem is that using a partial view and a view component for such purposes is slow and I would say overengineering. A short example
I would appreciate if you could show how your suggestion for using components would help simplify this situation. |
@sergei66666 what I'm pointing out is that we don't plan to make investments in this area, and that to achieve your goal you'll be better served with using Razor components, which are the lightweight alternative we recommend for "lightweight" partial views. |
Thanks. I decided to try Razor Components. I suffered a little to run it in a console application. The results are not encouraging. If we compare different implementations in terms of speed and memory, then:
ps. To call the Razor Component from |
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:
This will allow:
@functions
blockOption 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.
The text was updated successfully, but these errors were encountered: