ASP.Net MVC Controller cannot see another namespace - c#

I have an MVC Controller that cannot see another namespace in the using statement.
Specifically this line of code:
using TRN.Website.Tools;
Errors with:
The type or namespace name 'Tools' does not exist in the namespace
'TRN.Website' (are you missing an assembly reference?)
Other parts of my project can see the TRN.Website.Tools namespace however.
I tried adding the namespace to web.config but this had no effect.
EDIT: Sorry all I have missed out a vital bit of information. TRN.Website.Tools is just a folder with the namespace TRN.Website.Tools in the same project. It is not a separate project or a dll.

My guess: You have multiple projects in you solution and you have multiple libraries (dlls) that their names starts with TRN.Website and you added reference to one of these dlls in your MVC project. you have to add reference to the other one too.

Solved but in a really strange way.
I added another class in the Tools folder and after that the error disappeared.
Very very odd.

Check your Build Action of your class in properties window. It must be 'Compile'.

Your ASP.NET MVC project does not contain a reference to the assembly that contains the TRN.Website.Tools namespace. You have to reference that from your ASP.NET MVC project in order to use it.
If TRN.Website.Tools is a project in your Solution, you can just add a Project reference to it.
If it isn't a project in your Solution, you'll have to add a reference to the compiled DLL to your ASP.NET MVC project.

In my case, I had not checked in the "X.csproj" file to TFS, so the new controller I created was not being found in my testing environments as a valid controller.

Related

The type or namespace name 'X' could not be found

I have a CustomerOrderContext class in CustomerOrder.Data project in my solution. In another project, CustomerOrder.App, I have CustomerVM class. In this class I'm creating a CustomerOrderContext instance. But I get this following error:
Error 2 The type or namespace name 'CustomerOrderContext' could not
be found (are you missing a using directive or an assembly
reference?)
I have changed Build Actions of both projects to Page and then back to None and I have started to get this error. I have this line in CustomerVM:
using CustomerOrder.Data;
so I guess I shouldn't get this error. CustomerOrder.Data is also in References list of CustomerOrder.App. Can you tell me how I can fix this problem?
Thanks.
The issue is solved when I created new project, copied & pasted all classes.
be sure that CurstomerOrderContext class is marked as Public and not Internal.
Can you check if the framework versions are compatible? See https://stackoverflow.com/a/4286625/2478357.
Also, if you use Entity Framework (and CustomerOrder.Data uses EF), check if your CustomerOrder.App has a reference to the Entity framework dll. (This also applies to other imported dll's..)
you need to add Reference of that project (.dll) in your project.
You will find it when you right click your solution and add the (.dll) of other project in debug folder.
To access the class you must have same namespace
and
Check that both projects follow the same .Net Framework

I Can't access my class library in a web application

I have added Class Library in my web Project through Add reference option, but i am facing an error
Error 2 The type or namespace name 'UserDetailsDll' could not be
found (are you missing a using directive or an assembly reference?)*
I hope some one will help me.
Thanks in Advance.
There are a few possibilities as to why that is happening:
You're missing a using statement. Try using the whole name of the class (namespace, then class name) you're trying to use.
Your DLL is targeting a different processor architecture. Check the properties of your web application and your DLL and see if they are both targeting the same CPU.
Your DLL is for a different .NET framework than your web application. Check the target framework in the project properties.
Finally, check the references in Visual Studio for a little yellow warning icon next to your referenced DLL. It's presence indicates that the DLL couldn't be found / referenced in the first place.
You need to either add a using in your code file or specify the full name to the class you want to use.
Both solutions can be autogenerated. Rightclick on the class that won't get resolved and click on Resolve -> using XXXX

namespace not working in class library c#

I am trying to create a class library using some classes that I created in another project. One of the classes uses images and needs the System.Drawing namespace. However, when I try to copy the code from my project into a new class in my class library, I get an error saying the image object does not exist in the current context, and
The type or namespace name "Drawing" does not exist in the namespace System(are you missing a using directive or an assembly reference?)
It works fine in the other class as part of the other project. Why would this be?
Make sure that System.Drawing is added to the library's assembly references. By default, for class libraries, no Windows Forms assemblies are added.
Yes The problem lies in the References. If you go to your Solution Explorer and expand it, you will see a nested Folder Titled 'References'. Here is where you add the references needed in your project. To add them, simply right click the folder and select add Reference. Once here the reference you need will be in the .Net tab.
In case if a particular .NET reference is not added by default, do that manually:
using System.Drawing;
Write this in top of your class file. then also if u get error right click on reference and add reference of System.Drawing in your project.

Two routes after namespace refactoring MVC WebAPI

i renamed my namespace for my whole project. Everything is still working fine, but my WebAPI is now finding two routes for my controllers.
Multiple types were found that match the controller named 'department'.
This can happen if the route that services this request ('api/{controller}/{id}') found multiple controllers defined with the same name but differing namespaces, which is not supported.
The request for 'department' has found the following matching controllers:
A***.P***.Benutzerverwaltung.Jo***MVC.Controllers.DepartmentController
A***.Benutzerverwaltung.API.Controllers.DepartmentController
So i renamed the first namespace into the second, i searched everywhere for the old namespace, but nothing was found. So, what could i do?
Thanks for help.
So, what could i do?
Go to the bin folder of your web application and delete the old assemblies. ASP.NET loads all assemblies that are present in the bin folder. So if you are saying that you renamed some class library project reference, the old assembly is still physically there.

Type or namespace could not be found from App_code folder

I have written a class called ArchivedFilesWrapper in the App_code folder of my project, however when I use this class in another file in a different folder i get error:
The type or namespace name 'ArchivedFilesWrapper' could not be found (are you missing a using directive or an assembly reference?)
I thought every page should be able to find classes that are contained within the same project, but I guess this is not the case. Can someone please tell me what using statement I need to have?
Here is a snippet from my class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace EMCWebAdmin.App_Code
{
public class ArchivedFilesWrapper
{
Perhaps the problem will be solved by changing the Build Action Property of the *.cs source file to Compile from Content. From the Solution Explorer right click on the source file and choose Property.
Note that the App_Code folder is intended for use in Web Site Projects.
Note that for a Web Application Project or MVC project, adding an App_Code folder to your project and putting *.cs files in it will cause problems. I ignorantly added an App_Code folder to my MVC project from the Solution Explorer. VS defaulted the name space to MyProjectName.App_Code. In this case Visual Studio 2012 defaulted the Build Action to Content, even though the type was .cs code. After I changed Build Action Property of the *.cs source file to Compile from Content the namespace was resolved in other folder locations of the project. However because of problems, I had to change the name of folder--see below.
Important
In the MVC or Web Application project, the App_Code folder is trouble because it has Web Site Project type semantics. This folder is compiled when published (deployed) to the server. By changing Build Action from Content to Compile, you resolve the namespace issue on your development environment by forcing immediate compilation, but you get trouble when the second compilation results in objects defined twice errors on deployment. Put the code files in a folder with a different name. If you converted a Web Site to a Web Application, see the guidelines on the Net for this--not in the scope of this question. To read more about App_Code folder in the different project types see this blog
You need to add
using EMCWebAdmin.App_Code;
to all the pages you want to be able to use the class.
Alternatively you change the namesspace that the class is in to the same one that all the web pages use which presuming it is EMCWebAdmin
then in your class change
namespace EMCWebAdmin.App_Code
{
...
to
namespace EMCWebAdmin
{
...
This is a feature of visual studio, if you create a class in a folder structure, it uses a namespace that follows the folder structure.
If you convert it to a web app it should work. The downside is that it will no longer autobuild your code in app_code folder every time you change it and spin up the app. I have never seen a professional developer use a website project. I have no idea who MS were targeting when they created them.
Yes, put the calsses to another folder(not asp.net special folder), and only use the main namespace for the application is solve this issue.
Thanks johnmcp.

Categories