Error “A namespace does not directly contain membe

2019-01-11 21: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?

8条回答
时光不老,我们不散
2楼-- · 2019-01-11 22:12

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";
        }       
    }        
}
查看更多
成全新的幸福
3楼-- · 2019-01-11 22:16

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.

查看更多
登录 后发表回答