简体   繁体   中英

Can't find namespace (ASP.NET MVC 3)

I am currently working on an ASP.NET MVC3 project in Visual Studio. I have a solution set up that contains two seperate projects. I'm new to ASP.NET and I think I'm missing something basic as I cant reference classes out-with the Project that the class trying to reference is located in.

Just to clarify:

I have projectA that contains classA. I have projectB that contains classB.

I type this into classA:

using Solution.projectB.classB

I get an error saying that projectB does not exist.

项目A需要一个对项目B的项目引用,即右键单击项目A,选择添加引用,选择项目,然后选择项目B。

您需要将项目A中的参考添加到项目B中,反之亦然。

You should reference to the namespace and not directly to the class. Try changing your using to:

using Solution.projectB;

.NET can see namespaces across the same project. By default, it cannot see namespaces across different projects in the same solution. You need to add a reference from the top level project "A" to the dependent project "B".

How to: Add a Project Reference to a Visual Studio Web Project

After adding the reference, let Visual Studio do the heavy lifting. Type

classB instanceOfB = new classB(); (assuming there's a default constructor)

Use your mouse to click on classB. Hit the shortcut key CTRL + . . This will open up a drop down giving you the option to automatically import the namespace.

Visual Studio will use syntax coloring to let you know that the process was successful. Alternatively, use CTRL + SHIFT + B to compile the project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM