asp.net webpages content block and helper differences

Posted by metanaito on Stack Overflow See other posts from Stack Overflow or by metanaito
Published on 2011-03-18T05:04:23Z Indexed on 2011/03/19 0:09 UTC
Read the original article Hit count: 240

Filed under:
|

In asp.net webpages framework what is the difference between using a content block versus a helper?

They both seem to be used to output HTML to multiple pages. They both can contain code and both can pass parameters.

Are there other differences? When should you use a helper versus a content block?

More info:

With Content Blocks we create a .cshtml (for example _MakeNote.cshtml) file to hold the content we want to insert into a page. Then we use:

@RenderPage("/Shared/_MakeNote.cshtml") 

to insert the content into a page. We can pass parameters to the content block like this:

@RenderPage("/Shared/_MakeNote.cshtml", new { content = "hello from content block" })

It's somewhat like an include file, but I think does not share scope with the parent page.

With Helpers we create a .cshtml page in the App_Code folder (for example MyHelpers.cshtml) and place methods in that page which we want to call. The method looks something like this:

@helper MakeNote(string content) {
    <div>@content</div>
}

The helper is called by using:

@MyHelpers.MakeNote("Hello from helper")

© Stack Overflow or respective owner

Related posts about helpers

Related posts about asp.net-webpages