开发者

How to hide standard (existing) HtmlHelper

开发者 https://www.devze.com 2023-01-25 02:54 出处:网络
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.

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(..)%开发者_如何学Python> or any of its overloads.

Anybody having any clue if this is possible and how?


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);


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.


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.


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.


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()  %>
0

精彩评论

暂无评论...
验证码 换一张
取 消