Render Sections Using Razor View Engine

A small, simple and a handy one.

Recently, I came across the use of RenderSection method and learned about it due to a Web.Exception (mentioned below) that was thrown and had blown my mind.

Render Section! Let's first be familiar with this gem.

RenderSection() is a method from System.Web.WebPages namespace and the WebPageBaseclass. To be exact RenderSection() method is of two types, in other words the 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 that we will discuss below.

By the way, why is this required?

As we know something to explain has a better way of understanding if explained using an example. So, let's 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 the same that is the contacts of the Admin, and 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 in 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 it 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 we just define it and render it 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 where an important point about a Render Section comes that we should keep in mind before using one.

"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 about whether 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, in other words 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:

  1. @if(IsSectionDefined("SECTION NAME")) {    
  2.    @RenderSection("SECTION NAME")    
  3. }    
  4. else {    
  5. .....//Goes on    
  6. }   
People rightly say, "Man learns from Mistakes" and as a developer, we should be proud of these mistakes since this helps us learn more and more. :).

I hope this helps in some way.
References
ScottGu's web Blog


Similar Articles
Invincix Solutions Private limited
Every INVINCIAN will keep their feet grounded, to ensure, your head is in the cloud