Render Sections Magic using Razor View Engine

0.00 avg. rating (0% score) - 0 votes
A small, simple and a handy one..
Recently, I came across the use of RenderSection method & got to learn about this due to a Web.Exception (mentioned below) that was thrown and had blown off my mind.
Render Section!! Lets first know about this gem…
RenderSection() is a method from System.Web.WebPages namespace & the WebPageBaseclass. To be exact RenderSection() method is of two types i.e. same method but with different parameters. They are:-
  1. RenderSection(string):- This method just renders the contents of a named section in the layout Pages, as simple as this could be(the definition ūüôā ).
  2. RenderSection(string, Boolean):- This renders the named section similar to the previous method, but also specifies whether the section is required. This method with an extra parameter does a great bit of work, which we will discuss below.


Why is this required by the way!!
As we know something to explain has a better way of understanding if explained through an example. So, lets take one.
  • Suppose we are designing an application that has three pages. The “Home”, “About”, “Topics” pages and oviously all the pages have different content.
  • Now, we have a small sidebar on all the three pages and the content for Home and About pages are same that is the contacts of the Admin, & the Topics page has the contact replaced by a cloud structure displaying all the Tags for the articles/topics.
  • As we would know by now that these three pages will use the same layout and the sidebar would be displayed at the same place for the pages. Here is where the Render Section method comes into play.
  • In the layout.cshtml(C# Razor), wherever we need the sidebar section to be rendered, we write as @RenderSection(“Section Name”).
  • And in the individual pages those use the Layout, we define the section there as , @section “Section Name(remove the quotes)”{…..}. Wherever and whatever the contents might be just define it and render once in the Layout. This simplifies the work of writing the HTML codes and the css also..
This sounds so simple, until you have only three pages. Give a thought, is this possible always!! No. In a web application there might be many pages using The layout. Here is what an important point about Render Section comes, which we should keep in mind before using.
“Every Page that uses the Layout, and the layout.cshtml has a RenderSection(), that page needs to have the section with that name defined”. Else this also throws the user to the yellow screen of death(depending on the Custom Errors mode) or nothing a blank pageas whole parts of a page are not yet rendered. The exact exception is:-
“System.Web.Exception: Section not defined:”SECTION NAME”.”
Just give a thought if there are many pages, where would you search for, where you have missed to define the section. This is not a feasible solution right?
So, it is better to use the second method, i.e. RenderSection(string, Boolean).
If we use the RenderSection(“SECTION NAME”, required:false), what this means is, if a section is required and is not defined in the page and the section is optional in some page, then by the use of this method the RenderSection() method will render nothing and no runtime error would occur.
If we use the RenderSection(“SECTION NAME”, required:true), then we need to define the section in each page as we are prescribing here that section needs to be rendered on each page using the layout. We can also define a blank section to avoid the runtime error.
Using Conditional Detection for presence of a section:-
We can also use another small piece of code to avoid the runtime error and detect that the section is defined or not. Here goes the code:-

People rightly say, “Man learns from Mistakes” & as a developer, we should be proud of these mistakes as this helps us learn more & more. ūüôā

 Hope this helps some way..

Share on FacebookShare on Google+Share on LinkedInTweet about this on TwitterEmail this to someone