Error “A namespace does not directly contain members such as fields or methods”

我们两清 提交于 2019-11-27 09:00:37

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.

I managed to reproduce the error.

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

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.

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

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.

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.

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

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";
        }       
    }        
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!