views:

1255

answers:

1
  1. Create a new ASP.NET MVC Web Application
  2. Create an ASP.NET App_Code Folder
  3. Inside the new folder, create a class with an Extension Method. For example:

    static public class BugMVCExtension
    {
        public static int ToInt(this string str)
        {
            return Convert.ToInt32(str);
        }
    }
    
  4. Choose a View and try to use this new Extension Method

You will get this Exception:

CS0121: The call is ambiguous between the following methods or properties: '*MvcApplication1.App_code.BugMVCExtentions.ToInt(string)' and 'MvcApplication1.App_code.BugMVCExtentions.ToInt(string)*'

Anyone here has more information about it? Is it wrong to create an App_code in an ASP.NET MVC(?) Web Applications?

Thanks for you attention

+5  A: 

MVC projects created in Visual Studio use Web application project model by default. App_Code is mostly used by Web site model. I suggest reading about differences between them (another question covers this and it's also covered extensively on MSDN). If you add a source file to App_Code in a Web application project, Visual Studio will compile it to a DLL (since it's included in the project) and puts it in /bin. At run time, the ASP.NET compiler sees App_Code and tries to compile the source in a different assembly. As a consequence, two separate classes with identical names will exist in two different assemblies and when the ASP.NET parser tries to compile the .aspx file, it'll fail to choose one.

Update:

Are those two (extension method and the class you're instantiating) in a single .cs file? Otherwise, probably, the class you're instantiating is in a source file with Build Action (right click on file, click properties) set to Content which tells Visual Studio to skip it in the build process (in that case, you won't be able to reference it in other .cs files that are outside App_Code but you'll be able to use it in the view since it'll only come to life at run time.) If the build action is Compile, you'll get an error. The issue is definitely not specific to extension methods. Visual Studio seems to be smart enough to set it to Content by default for source files added to App_Code.

Mehrdad Afshari
But why this problems only happens if I try use the Extention Method inside a View? For example, if I try use this same EM in a Controller this runs without problems.
Cleiton
Because the Controller is also compiled at build time by VS compiler. At that time, there's only one instance of that class. `App_Code` is compiled at **run time**.
Mehrdad Afshari
Mehrdad, I understand, but if it happens like it should be, I was supposed to get this compiler exeption "even" I DON'T USE the extention method or not? Do another test, create an instance class inside App_code, try to use that in another view... It will work without problems. I dont understand why this only happens with extention methods.
Cleiton
No, you only get it when you use the extension method. If you don't there would be two different methods living happily in different assemblies. Unless someone calls it, you never get to choose one. Can you describe the second situation (create instance... I don't get it)
Mehrdad Afshari
I'm sorry my poor english :) Before I start get this compiler exception I was using some classes that was created inside app_code without problems, that is because i Thought it was strange because it only happens with extetion methods.
Cleiton
It doesn't happen just with extension methods! It should happen if you have a class in App_Code that's also compiled in VS project and you are referencing its name **directly** in the .aspx file.
Mehrdad Afshari
@Mehrdad, but it does! What does you mean with directly? I've tried using Import directive and full classified naming conversion (MySite.App_code.NameOfClass)
Cleiton
@Mehrdad,that was that! Thank you very much! Now I understand the source of the problem.
Cleiton