0

I have created a set of custom HtmlHelper extensions.

I would like to know if it is possible to prevent other developers in the same project from using the standard HtmlHelpers.

Eg: Say, I have..

public static string CustomDropDown(this HtmlHelper html)
{
   //Custom code here
}

I want only <% Html.CustomDropDown(..)%> to be visible to developers and not the standard <% Html.DropDownList(..)%> or any of its overloads.

Anybody having any clue if this is possible and how?

5 Answers 5

3

Consider using EditorTemplates and/or DisplayTemplates instead. This way, all your developers need to do is to always call the

Html.EditorFor(model => model.MySpecialProperty);

or

Html.DisplayFor(model => model.MySpecialProperty);
0
1

Why not just tell them?

"Hey guys, these custom helpers are a lot better and you should use them".

If you have management support this shouldn't be a problem.

0

I doubt there is any failsafe solution and maybe it's not a very wise thing to do either...

But you could consider taking out the

<add namespace="System.Web.Mvc" />

section out of the in the web.config.

4
  • ...and watch as your solution fails to compile. Oops. A little too radical, methinks.
    – spender
    Nov 11, 2010 at 12:13
  • Hmmm, I've seen somebody suggest the EditorVisible() attribute on classes, but just cant seem to find that post again on stackoverflow. Was thinking it somehow has something to do here.. Nov 11, 2010 at 12:33
  • Shouldn't fail to compile with skipping that line. At most maybe failing at runtime... Nov 11, 2010 at 13:16
  • I did try this before I posted this question. Not satisfactory, I must say, since I still want other things in System.Web.Mvc to be accessible. Nov 11, 2010 at 15:00
0

ASP.NET MVC is an open project - you can download source code and manually remove some methods from HtmlHelper class, rebuild and supply this library to your developers.

1
  • That was my first impulse. :) But I want to target only specific parts of the solution. Nov 11, 2010 at 15:01
0

Could you not just refactor your code??

public class CustomControls
{
    private readonly HtmlHelper _helper;
    public CustomControls(HtmlHelper helper)
    {
       _helper = helper;

    }

    public static string DropDown()
    {
       //Custom code here
    }
 }

 public CustomControls CustomControls(this HtmlHelper helper)
 {
    return new CustomControls(helper);
 }

Usage:

<%:  Html.CustomControls().DropDown()  %>
2
  • But that doesn't affect the visibilty or usage of Html.DropDownList, does it? Nov 24, 2010 at 11:41
  • No, but the assumption was you only wanted developers to see a list of your extensions instead of a complete list (where others may have created control of the same name).
    – Lee Smith
    Nov 24, 2010 at 12:20

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.