Extension methods not showing

I am creating extension methods for the HtmlHelper class in an MVC web application. Nothing is displayed, not even the standard InputExtensions.

public static class HtmlHelpers
{
    public static void RegisterScriptInclude(this HtmlHelper htmlhelper, string script)
    {
        if (!RegisteredScriptIncludes.ContainsValue(script))
        {
            RegisteredScriptIncludes.Add(RegisteredScriptIncludes.Count, script);
        }
    }

    public static string RenderScripts(this HtmlHelper htmlhelper)
    {
        var scripts = new StringBuilder();
        foreach (string script in RegisteredScriptIncludes.Values)
        {
            scripts.AppendLine("<script src='" + script + "' type='text/javascript'></script>");
        }
        return scripts.ToString();

    }

    private static SortedList<int, string> RegisteredScriptIncludes
    {
        get
        {
            SortedList<int, string> value = (SortedList<int, string>)HttpContext.Current.Items["RegisteredScriptIncludes"];
            if (value == null)
            {
                value = new SortedList<int, string>();
                HttpContext.Current.Items["RegisteredScriptIncludes"] = value;
            }
            return value;
        }
    }

}

Extension methods are also not displayed in code.

Where are they?

+3
source share
2 answers

Did you forget the operator using? In particular, you will need " using path.to.my.namespace;" to get extension methods.

+8
source

Another obvious thing to check if someone finds this message is to forget the keyword "this" in the first argument of your extension method. If you forget that the compiler cannot help you!

+3
source

Source: https://habr.com/ru/post/1710426/


All Articles