brando
brando

Reputation: 8431

Namespace conflicts in Asp.net MVC

I am a long-time semi-professional asp.net programmer who has recently made the leap from asp.net forms environment to MVC. So I am in the process of re-developing one of my web apps which exists in forms to MVC, and I am really liking the MVC environment and am glad I am making the leap.

But...am encountering some issues, like this one with namespaces.

So let's imagine the following: 1) I create a new MVC 4 project called MyMvcProject 2) I immediately create the App_Code folder and add to it a new class1.vb file as follows:

Namespace MyNamespace
  Public Class Class1
      Property MyProperty As String

  End Class
End Namespace

Ok so far so good. Now I want to access this class in a controller or something as follows:

Imports MyNamespace

Public Class Default1Controller
   Inherits System.Web.Mvc.Controller
     Function Index() As ActionResult
        Dim obj As New Class1  

        Return View()
     End Function
End Class

In the asp.net forms environment, I would be good to go. However, in my MVC project i am unable to import "MyNamespace" and therefore unable to create Class1. So I go looking for it and find the following in the object browser:

>[OTHER REFERENCES...]
v[VB] MyMvcProject
      >{}MyMvcProject
      >{}MyMvcProject.My
      >{}MyMvcProject.My.Resources
v[VB] MyMvcProject
      v{}MyNamespace
         > [] Class1
>[OTHER REFERENCES...]

So there's my "MyNamespace" and "Class1." So I attempt to revise the code as follows:

Imports MyMvcProject.MyNamespace

Public Class Default1Controller
   Inherits System.Web.Mvc.Controller
     Function Index() As ActionResult
        Dim obj As New Class1  

        Return View()
     End Function
End Class

But THIS doesn't help. I am only able to reference the FIRST instance MyMvcProject and it's sub-references. Not the second where my "Class1" resides. I am uncertain as to how to fix this.

UPDATE 1: I FOUND THE SOLUTION (BUT NOT THE EXPLANATION) For whatever reason, ASP.NET MVC does not like me to put stuff in the App_Code folder. However, if i put the class1.vb in the MODELS folder then I can I can import "MyMvcProject.MyNamespace" and proceed normally. The object browswer now looks like this:

>[OTHER REFERENCES...]
v[VB] MyMvcProject
  >{}MyMvcProject
  >{}MyMvcProject.My
  >{}MyMvcProject.My.Resources
  v{}MyMvcProject.MyNamespace
     > [] Class1
v[VB] MyMvcProject
>[OTHER REFERENCES...]

Notice the second reference to MyMvcProject STILL exists. But "MyNamespace" and the classes i put into it are under the FIRST instance, which I can reference and import normally. Kind of quirky, it seems to me. But if i have to throw everything in the MODELS folder, well, I'll do it.

UPDATE 2 Had I know the problem was with putting stuff in the App_Code folder, I could have found some answers: ASP.NET MVC using App_Code directory

UPDATE 3

All MVC project by default are Web Application Projects (WAP) instead of Web Site projects. This means that there's no need for an App_Code folder since WAPs always get compiled anyway. That means that all *.cs files in your project will get compiled, as opposed to Web Site projects where only *.cs files in your App_Code folder would get compiled.

Source: http://forums.asp.net/t/1315143.aspx?MVC+and+App_Code+folder

UPDATE 4 And here is how to include the App_Code folder in your MVC project:

you have to go at Properties section of the file and then for Build Action change from Content to Compile. That is it ;)

Source: http://how-to-code-net.blogspot.mx/2014/04/appcode-classes-not-available-in-aspnet.html

Upvotes: 0

Views: 608

Answers (0)

Related Questions