Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What does the class declaration look like? Make sure you have made the class itself <code>static</code> as well:</p> <pre><code>public static class MyHelpers { public static string OutputBlah(this HtmlHelper helper) { return helper.InnerWriter.ToString(); } } </code></pre> <p>And then use the regular <code>Html</code> property of type <code>HtmlHelper</code> in the <code>View</code>:</p> <pre><code>&lt;%= Html.OutputBlah() %&gt; </code></pre> <hr> <p>Answer to follow-up question from OP:</p> <p>When declaring a method like this (<code>static</code> method in <code>static class</code>, and with the first parameter with the <code>this</code> keyword), you define an <a href="http://msdn.microsoft.com/en-us/library/bb383977.aspx" rel="nofollow noreferrer"><em>Extension Method</em></a> - a feature that was introduced in C# 3.0. The basic idea is that you define a method that is hooked into another class, thus <em>extending</em> it.</p> <p>In this case, you're extending the <code>HtmlHelper</code> class (becuase that is the type of the <code>this</code> parameter), and thereby making <code>.OutputBlah()</code> available on any instance of <code>HtmlHelper</code>. If you examine the <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.viewpage.html.aspx" rel="nofollow noreferrer"><code>Html</code></a> property of the <a href="http://msdn.microsoft.com/en-us/library/system.web.mvc.viewpage.aspx" rel="nofollow noreferrer"><code>ViewPage</code></a>, you'll notice that it is in fact of type <code>HtmlHelper</code>.<br> So when you use <code>Html.OutputBlah()</code> in your view, you're actually accessing the <code>HtmlHelper</code> instance contained in the viewpage's <code>Html</code> property, and calling your own extension method on it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload