6

I hope somebody can help me. I've been trying to write a custom html helper for my MVC application. First at all, i tried with a testing one, which only writes a

tag for the specified param. The things is, it does not work unless I explicitly import the namespace. I've been reading a lot and as i read, That method should appear without the import namespace like this:

<%=Html.Prueba("This is a paragraph") %>

But this method, Prueba, is not showing up in the VS Intellisense.

My Class is the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace EasyGoCostaRica.Helpers
{
    public static class ViewsHelpers
    {
        //This method is just for testing. Is not working :(
        public static string Prueba(this HtmlHelper helper, string param1)
        {
            return string.Format("<p>{0}</p>", param1);
        }
    }

}

Thanks in advance!

4
  • Correct your code in the first code block. You are using ampersand instead of percent Commented Sep 15, 2009 at 5:48
  • Yeah. It's the Ameprsand here: <&=Html.Prueba("This is a paragraph") %> Commented Sep 15, 2009 at 6:08
  • Lol That's not the problem, I write here by hand, not copying-paste, the problem was the namespace, just added to the web.config and voilà! Commented Sep 15, 2009 at 6:15
  • I didn't point out your ampersand because I thought it's not working because of it, but because it's easier to read formatted code, when it's properly written. Happens all the time. :) Commented Sep 15, 2009 at 7:09

3 Answers 3

13

Namespace must be declared/imported somewhere. You can do that either:

  • within the page itself
  • master page or
  • inside web.config file

If you want something global it's best to configure your namespace in web.config.

Use <@import...> directive for the first two and <namespace> configuration element for the last one.

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

1 Comment

Thanks all! I didn't consider the web.config earlier... Thanks!
7

For some reason in visual studio 2013 you have to restart vs in order for changes in the web.config to be applied.

4 Comments

I've found the same in VS 2012! And even then it's touch and go.
Had this problem in VS 2015 too.
Same thing in VS 2015 for me, this is awful .
VS 2019 apparently has this problem as well. Restart needed before it registers the change in Web.config
6

You can add the namespace to the web.config and then you won't have to worry about it later.

Inside your web.config, you should see something like this:

<namespaces>
<add namespace="System.Web.Mvc"/>
<add namespace="System.Web.Mvc.Ajax"/>
<add namespace="System.Web.Mvc.Html"/>
<add namespace="System.Web.Routing"/>
<add namespace="System.Linq"/>
<add namespace="System.Collections.Generic"/>
</namespaces>

Just add a line with your namespace.

If you don't want the helpers to be globally imported, each directory can have it's own web.config. Unless specifically set, those "sub" web.configs will inherit settings from higher web.configs. If you go this route, be forewarned, some settings can only be set at the application level. It can get confusing fast.

1 Comment

I've added the namespace to the web.config file in the Views folder. My new helper does work in any of the view files apart from the _Layout.cshtml file. Why would that be?

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.