在ASP.NET MVC中添加我自己的类文件,但无法引用?
我目前正试图将我自己的类文件添加到 ASP.NET MVC 项目中,所以它可以 由我的控制器引用。但 Visual Studio 2010 总是抱怨“无法找到类型或名称‘产品’(您是否缺少 using 指令或程序集引用?)
我不确定将我的类文件放在哪里,并尝试将类添加到模型或控制器我的类位于正确的命名空间下,并且我确实在我的控制器中引用了模型命名空间,这可能是显而易见的事情,但我无法让它工作:(
I am currently stuck trying to add my own class file into ASP.NET MVC project, so it could
be referenced by my controller. But Visual studio 2010 always complain about "The type or name 'Products' could not be found (are your missing a using directive or assembly reference?)
I am not sure where to put my class file and tried add the class to Models or Controllers directory, and it wouldn't work either. My the class is under the right namespace, and I did reference the Models namespace in my Controller. It could be something obvious, but I couldn't get it working:(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
确保文件的“构建操作”属性在 Visual Studio 中设置为“编译”。也许当您最初添加文件时,您将其添加到某个位置或将其添加为不会自动正确设置构建操作的文件类型。
Make sure that the "Build Action" property of the file is set to "Compile" in Visual Studio. Perhaps when you initially added the file, you added it to a location or added it as a file type that did not automatically set the build action properly.
一般的经验法则是,业务对象位于正确的 Models 文件夹中。如果它不起作用,则说明项目配置不正确。在您想要引用 Products 对象的控制器中,您是否添加了对 Models.Products 的引用?
列表中还有其他错误吗?有时,解决方案中的多个错误可能会结合起来产生类似的结果。
The general rule of thumb is that business objects go it the Models folder you're correct. If it isn't working you don't have the project configured correctly. In the controller where you wanted to reference the Products object, did you add a reference to Models.Products?
Are there any other errors in listed? Sometimes multiple errors in a solution can combine to create something like this.
确保类的命名空间正确。
Ensure the namespace of your class is correct.
根据对不同答案的评论,您似乎正在尝试添加实用程序或帮助程序类。我建议您将其放在项目根目录下的 Helpers 文件夹中。
所以首先在项目根目录下添加Helpers文件夹。完成后,您应该具有以下结构(也可能还有其他文件夹):
现在,将一个新类添加到 Helpers 文件夹中。将该类命名为 ProductsHelper(在“添加新项”对话框中,确保输入 ProductsHelper.cs,因为它要求提供文件名称)。
现在查看新的 ProductsHelper.cs 文件。复制在所述文件中找到的命名空间,以在控制器中使用。
现在,在控制器顶部添加一个 using 指令,如下所示:
有时,项目设置为使用与项目名称不匹配的默认命名空间,在这种情况下,新添加的类文件中的命名空间可能是与您认为应该添加为 using 指令的内容不同。如果您想知道项目的默认命名空间是什么,可以在项目属性的“应用程序”选项卡上的“默认命名空间”文本框中看到它(您也可以在此处更改它)。
Based on a comment to a different answer, it appears you're trying to add a utility or helper class. I suggest you put it in a Helpers folder under the root of the project.
So first add the Helpers folder under the root of the project. You should have the following structure when done (there may be other folders as well):
Now, add a new class to the Helpers folder. Call this class ProductsHelper (in the Add New Item dialog make sure to put ProductsHelper.cs, as it's asking for the file name).
Now have a look in the new ProductsHelper.cs file. Copy the namespace found in said file, to be used in the controller.
Now, add a using directive at the top of your controller that looks like the following:
Sometimes projects are set up to use default namespaces that don't match the project name, in which case the namespace in your newly-added class file might be different than what you thought you should add as the using directive. If you're wondering what the default namespace for your project is, you can see it in the project's properties, on the Application tab, in the "Default namespace" textbox (you can change it here as well).
我在向新的 MVC 项目添加一些实用程序类 .cs 文件时遇到了同样的问题。
我主要从事 VS 2010 网站项目,其编译方式与
Web 应用程序项目 (WAP),例如 MVC 3 项目。
只是为了澄清 Jacob 的答案,在 VS 解决方案资源管理器中:右键单击类 .cs 文件并选择属性对话框,然后设置属性:构建操作以编译。
这将在您构建项目时强制编译代码。
HTH,洛杉矶盖伊
I had the same problem adding some utility class .cs files to my new MVC project.
I was working mostly on VS 2010 Website projects which compile differently from
Web Application Projects ( WAP ) like the MVC 3 Project.
Just to clarify Jacob's answer, In the VS Solution Explorer: right click on the class .cs file and select the Property Dialog and then set the property: Build Action to Compile.
This will force the code to be compiled when you Build the project.
HTH, LA Guy