37

I'm trying to build my C# project and I'm getting the error message "A namespace does not directly contain members such as fields or methods". It is flagging the first character (the less than symbol) of the app.config file.

I've checked all of my files for places where there are variables or functions directly inside of a namespace--found nothing. The app.config looks fine.

Google is failing me and I'm pulling my hair out. What could be causing this error?

Chris Moschini
  • 36,764
  • 19
  • 160
  • 190
BoltBait
  • 11,361
  • 9
  • 58
  • 87

8 Answers8

57

Figures! As soon as I finally break down and ask the question that I find the answer...

The app.config file properties section (somehow) listed the Build Action of "Compile" when it should be set to "None".

How in the world did it get changed? I know I didn't change it. Grrr...

Oh well, at least it's building now. Hopefully someone else will benefit from my hairloss.

BoltBait
  • 11,361
  • 9
  • 58
  • 87
  • Yes you did. Now, where were you yesterday?! – BoltBait Jan 19 '10 at 01:07
  • 1
    Tearing my hair out over some other silly issue. Most of these lessons are ones learned from firsthand experience :/ – Anon. Jan 19 '10 at 01:11
  • My hair is growing back, thanks mate! @BoltBait (bolt before?) – Langeleppel Oct 16 '13 at 19:25
  • 1
    This will occur if ANYthing that should not be compiled switches to Build Action = Compile, not just app.config. In our case, it was a single LESS file, which ironically is "compiled" as in Web Essentials, but explodes when "compiled" as in Visual Studio Build Action. – Chris Moschini Jul 15 '14 at 22:16
  • This can happen for typescript too if you create a file as `.cs` and then rename it to `.ts` – Simon_Weaver Jun 27 '17 at 18:17
5

I managed to reproduce the error.

Check the properties for the app.config file. The Build Action should be None, not Compile.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
0

This error occurs when you attempt to add a field or member directly into a namespace. Given it's pointing to app.config it's likely that VS is in a bad state. Try closing and reopening Visual Studio and see if the error goes away.

Failing that try rebuilding and posting the results from the build output window to your question.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454
0

I solved this way: right click on .axml file -> Build Action ->AndroidResource (xamarin studio)

alin0509
  • 451
  • 4
  • 5
0

I had the same error. Checked if the app.config was set to none, no problems there. After 30 minutes of checking and rechecking, I restarted VS and that did the thing. It usually solves 90% of the unlogical errors. Sighs.

Zwadderich
  • 251
  • 3
  • 10
0

I was having a similar problem with the code behind in my website. The issue turned out to be an extra closing bracket that caused my class to end right after page_load(). Always check for silly syntax errors like this one.

0

I was facing this same issue with XML file. This was due to .net compiler tried to compile this xml which has been used only for DATA purpose hence no need of compilation.

Fix : Go to property of this file and change the Build Action to content from compile.

Thanks

Siva
  • 23
  • 5
0

In my case i was by mistake putting a new function outside the controller class. This was my code in ASP.NET MVC that was giving this error.

public class HomeController : Controller
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }       
    }

    [HttpPost]
    public string Insert(string name, string age, string standard, string percent, string address, string status)
    {
        //some code
        return "value";
    }
}

It should be like:

public class HomeController : Controller
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public string Insert(string name, string age, string standard, string percent, string address, string status)
        {
            //some code
            return "value";
        }       
    }        
}
yogihosting
  • 5,494
  • 8
  • 47
  • 80