6

I am trying to use a string extension method in a partial view. I get the following error:

'string' does not contain a definition for 'TruncateAtCharacter'

Here is the extension method:

namespace PCCMS.Core.Libraries {
    public static class Extensions {
        public static string TruncateAtCharacter(this string input, int length) {
            if (String.IsNullOrEmpty(input) || input.Length < length)
                return input;

            return string.Format("{0}...", input.Substring(0, length).Trim());
        }
    }
}

According to this previous question I need to add the namespace to web.config, however I have done this and I still receive the same error message. What's odd though, is that I do get intellisense for the extension method?

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="PCCMS.Core.Libraries.ClientWebViewPage">
        <namespaces>
            <add namespace="System.Web.Mvc" />
            <!-- Other namespaces... -->
            <add namespace="PCCMS.Core.Libraries" />
        </namespaces>
    </pages>
</system.web.webPages.razor>

Can anyone explain why this is?

Thanks

Community
  • 1
  • 1
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339

2 Answers2

1

This should work if the namespace declaration is in the system.web.webPages.razor/namespaces element of your root Views directory web.config. If that fails, try using an explicit @using statement at the top of the View without any web.config statements. It 'should' work.

PS Is that ReSharper intellisense or VS? ReSharper explicitly tells me that an @using is required if the web.config entry is not in scope.

Rob Kent
  • 5,183
  • 4
  • 33
  • 54
  • Sorry, I should have mentioned I tried adding an @using in the partial and that did not work. Neither has adding the namespace declaration to the web.config within the area, nor the one in the project root. – Rory McCrossan Jul 17 '12 at 14:54
  • I just copied your code and it only works if I use an @using statement in the view. Just referencing the namespace in either configs doesn't do anything. – Rob Kent Jul 17 '12 at 14:57
  • Okay. I just got it to work only using the web.config in the Views directory and with no @using statement in the View itself. I am using the system.web.webPages.razor/namespaces element. I didn't have that element in my root config and when I added the statement to the default system.web.pages as well, it confused the parser. Just try putting it in the Views web.config. – Rob Kent Jul 17 '12 at 15:01
  • 1
    @RoryMcCrossan, when you say the *project root* do you mean `~/web.config` or `~/Views/web.config`? Because you have to declare the namespace in `~/Views/web.config` if you are using the Razor view engine. – Darin Dimitrov Jul 17 '12 at 15:08
0

Is the error occurring when running in VS debugger, or from a test or production system? Make sure your module containing the extension is properly installed (and updated properly). Also, try running "iisreset" from the command prompt.

Les
  • 10,335
  • 4
  • 40
  • 60