4

how I can create something like this: Html.MyApp.ActionLink()? Thanks.

2
  • In that context, what is MyApp? A type? A local object? A controller? A route? A view? What? Commented Feb 19, 2011 at 19:28
  • I would like to have a root for all of my helpers. So instead to have helpers like Html.MyHelperForLinks or Html.MyHelperForText, I want this: Html.MyApp.MyHelperForLinks, Html.MyApp.MyHelperForText, Html.MyApp.MyHelperForTextBox, and so on... Commented Feb 20, 2011 at 6:48

1 Answer 1

7

You can't do this. The only way you can add to the Html class is via an extension method. You cannot add "Extension Properties", which is what would be required for you to use Html.MyApp. The closest you could come is Html.MyApp().Method(...)

Your best bet is probably to either include them as extension methods on Html, or create a new class completely (eg. MyAppHtml.Method(...) or MyApp.Html.Method(...)). There was a blog post around recently specifically showing an "Html5" class with these methods, but unfortunately my Google skills are failing me, and I can't find it :(

Added 13 Oct 2011 as asked in comment

To do something like Html.MyApp().ActionLink() you need to create an extension method on HtmlHelper, that returns an instance of a class with your custom method:

namespace MyHelper
{
    public static class MyHelperStuff
    {
        // Extension method - adds a MyApp() method to HtmlHelper
        public static MyHelpers MyApp(this HtmlHelper helper)
        {
            return new MyHelpers();
        }
    }

    public class MyHelpers
    {
        public IHtmlString ActionLink(string blah)
        {
            // Wrap the html in an MvcHtmlString otherwise it'll be HtmlEncoded and displayed to the user as HTML :(
            return new MvcHtmlString(string.Format("<a href=\"#\">{0}</a>", HttpUtility.HtmlEncode(blah)));
        }
    }
}

Note: You'll need to import the namespace this class is in in Web.config, like this:

<?xml version="1.0"?>
<configuration>
    <system.web.webPages.razor>
        <pages>
            <namespaces>
                <add namespace="MyHelper"/>
            </namespaces>
        </pages>
    </system.web.webPages.razor>
</configuration>

The change in the Web.Config file needs to be done in the config file for the view, not the global one

Sign up to request clarification or add additional context in comments.

7 Comments

If this answers the question, you're welcome to mark it as the answer ;)
How I can do something like this "Html.MyApp().Method(...)"?
Sure, but there is an error. "Extension method must be defined in a non-generic static class", for both MyHelperStuff and MyHelpers classes.
Sorry, you need to mark the class as static (updated my sample)
Does not work. The same error for the "MyHelpers" only. I tried to change something else with no lucky.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.