简体   繁体   中英

Too many sub-namespaces in Asp.Net? CS0234

Working on an Asp.Net project, adding a new control (ascx), run into a bizarre CS0234 issue. Basically, the compiler can't find a fully qualified namespace. What makes it really bizarre is that sometimes it can find it, and sometimes it can't. Each time I do a build it fails to find it in different files. That is, sometimes it builds. Sometimes it finds it in files a and b, but not c an d. Sometimes it finds the namespace in a, b, and c, but not d. And every other permutation.

Add a new control defined in class ClassName (ClassName.ascx and ClassName.ascx.cs files).

The path is: c:..\\Project\\WebuserControls\\ControlType

The namespace is Project.WebUserControls.ControlType

Throughout the project I call Project.WebUserControls.ControlType.ClassName.SomeMethod(); When I compile I get error "CS0234 The type or namespace name 'Project.WebUserControls.ControlType' does not exist in the class or namespace 'Project.WebUserControls' (are you missing an assembly reference?)" even though it most certainly does exist in that namespace. Also, Intellisense can't find the namespace, or anything in it.

The c# file is of the form

namespace Project.WebUserControls.ControlType
{
    using System 
    ...etc
    public partial class ClassName : System.Web.UI.UserControl
    {

After much googling about I find this , which maybe leads to a solution.

In web.config:

<system.web>
    ...
    <pages>
        ... 
        <namespaces> 
            <add namespace="Project.WebUserControls" />

Interestingly, if I replace namespace="Project.WebUserControls" with namespace="Project.WebUserControls.ControlType" it won't compile.

This fix works for about half an hour, and several builds, but has now stopped working. No, I didn't change any code to make it stop working.

Any ideas?


A couple hours later: Moving ClassName, both the class and the files, up one level in the namespaces and path respectively seems to have fixed the problem. That is, it's now in namespace Project.WebUserControls and on path c:..\\Project\\WebUserControls\\

No idea why this worked. Path name was well under 255 characters.


Next day, broken again... Didn't change anything.

Sounds like the shadow copy that vstudio/IIS does isn't being rebuilt/used properly.

Try the built in developer server (in vstudio) or restart IIS

Sounds more like Visual Studio issue than C#/.NET issue to me.

Just to clarify: you get those errors only after you add some class then try to access it, or at random after each build even if you didn't add anything?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM