Razor Secrets

I was wondering if anyone here could shed some light on some of the questions I had recently about Razor.

1) In principle, I wanted to divide the definition of the section into several parts? What for? Because I wanted to.

Unfortunately, using @section "sectionname" threw an exception twice.

Looking deeper into the source of Razor, I saw that Razor checked if the partition was already defined and threw an exception if it was already defined.

Since Razor uses delegates to render sections, I changed the code to use Delegate.Combine (if the section was defined earlier) and it worked, now I was able to create a section that was declared twice (or more).

2) When a section is defined in the view but not displayed on the layout page, an Exception is thrown. I have not seen an elegant solution to overcome this. If I want to use a view page with multiple layout pages ... This is kind of a problem.

Looking at the source (again), there is only a foreach that checks whether each section defined in the view has been rendered, and then it throws an exception if the section has not been rendered.

I thought of some solutions (especially for my second question, inheriting from the base class view and exposing unpublished sections to the layout page and doing some processing), but they seem ... like hacks. Are there any better solutions that actually change the source?

, - , , Razor , , , , .

@davidferguson @takepara, . , , , . , .

+3
3

1) , , , . , , .

2) . , . , vNext, . ( promises, , )

+5

, , .

@RenderSection("sectionname", required: false)

, - , .

+5

.

+1

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


All Articles