asp.net-mvc Share @helpers across views


Example

@Helpers could be shared between views.

They should be created in the folder App_Code

enter image description here

@helper CreatePrimaryBootstrapButton(string label)
{
    <button type="button" class="btn btn-primary">@label</button>
}

//call

@MenuHelpers.CreatePrimaryBootstrapButton("my button")

The globals @Url and @Html aren't available by default in the @Helper defined in App_code. You could add them as follows (for every .cshtml in your App_code folder)

@*  Make @Html and @Url available *@
@functions
{
    private new static HtmlHelper<object> Html
    {
        get { return ((WebViewPage)CurrentPage).Html; }
    }

    private static UrlHelper Url
    {
        get { return ((WebViewPage)CurrentPage).Url; }
    }
 }