1

I can't figure out why I'm getting this error. I only have one class with one method in my project that looks like:

public static class Extensions
{
    public static string Chop(this string s, int length)
    {
        ...
        return "";
    }
}

I'm calling this in my View like so: @item.PostContent.Chop(20)
It's giving me a compiler error:

The call is ambiguous between the following methods or properties: 'Extensions.Chop(string, int)' and 'Extensions.Chop(string, int)'

Any help is appreciated. Thank you.

Rivka
  • 2,172
  • 10
  • 45
  • 74
  • If the method is static don't you have to call it on the class name? `@item.PostContent.Extensions.Chop(20)`? – Hunter McMillen Mar 22 '12 at 01:01
  • I didn't think so. I was using this article as a reference: http://www.mikesdotnetting.com/Article/137/Displaying-The-First-n-Characters-Of-Text. Either way, the compiler doesn't like that... – Rivka Mar 22 '12 at 01:49
  • In the article he calls the code from his view like this: `ViewData["myString"].ToString().Chop(50)`, did that not work in your case? – Hunter McMillen Mar 22 '12 at 01:57

1 Answers1

5

I'm guessing that you have put your Extensions class file in an App_Code folder, is that correct? If so, move it outside of App_Code and get rid of the folder. App_Code has no place in MVC applications which by default are created as Web Application projects as opposed to Web Site projects.

Web Applications are compiled to a dll at build time, and anything in App_Code is also compiled into a separate dll at run time. Hence there being two methods and the ambiguity.

Mike Brind
  • 28,238
  • 6
  • 56
  • 88