Custon Razor HtmlHelper conflicts with namespace at runtime

I created a custom WebViewPage

. This allows me to add a custom helper to use in my Razor views:

public abstract class CompanyNameWebViewPage<TModel> : WebViewPage<TModel>
{
    public override void InitHelpers()
    {
        base.InitHelpers();
        CompanyName = new CompanyNameHtmlHelper<TModel>(this.Html);
    }

    public CompanyNameHtmlHelper<TModel> CompanyName { get; protected set; }
}

      

Using it in my Razor as such:

@CompanyName.Theme.Image("logo.png")

      

The problem is that CompanyName

- this is both the name I really want to use for the HTML helper and the namespace that our code goes into. So in visual studio @CompanyName.Theme.etc

mine is resolved CompanyNameHtmlHelper

.

However, at runtime, I get an error:

The type or namespace name 'Theme' does not exist in the namespace 'CompanyName' (are you missing an assembly reference?)

      

From this you can see what CompanyName

now resolves namespace instead of HtmlHelper.

Why is this happening? Why does Visual Studio let me do this when it gives me a runtime error? Is there work around? It's really annoying when you have to come up with some confusing name, not just my company name.

+3


source to share





All Articles