Asp.Net 中子命名空间太多? CS0234

发布于 2024-10-02 09:10:56 字数 1624 浏览 0 评论 0原文

在开发一个 Asp.Net 项目时,添加一个新的控件 (ascx),遇到了一个奇怪的 CS0234 问题。基本上,编译器找不到完全限定的命名空间。让它真正奇怪的是,有时它能找到它,有时却找不到。每次我进行构建时,都无法在不同的文件中找到它。也就是说,有时它会构建。有时它在文件 a 和 b 中找到它,但在 c 和 d 中找不到。有时它会在 a、b 和 c 中找到命名空间,但在 d 中找不到。以及所有其他排列。

添加在 ClassName 类(ClassName.ascx 和 ClassName.ascx.cs 文件)中定义的新控件。

路径是: c:..\Project\WebuserControls\ControlType

命名空间是 Project.WebUserControls.ControlType

在整个项目中我调用 Project.WebUserControls.ControlType.ClassName.SomeMethod(); 当我编译时出现错误“CS0234 类型或命名空间名称 'Project.WebUserControls.ControlType' 在类或命名空间 'Project.WebUserControls' 中不存在(您是否缺少程序集引用?)”,即使它肯定存在于该命名空间中。此外,Intellisense 无法找到名称空间或其中的任何内容。

c# 文件的形式

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

经过多次谷歌搜索后,我发现 this ,这可能会带来解决方案。

在 web.config 中:

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

有趣的是,如果我将 namespace="Project.WebUserControls" 替换为 namespace="Project.WebUserControls.ControlType" ,它将无法编译。

此修复工作大约半小时,并进行了几次构建,​​但现在已停止工作。不,我没有更改任何代码以使其停止工作。

有什么想法吗?


几个小时后: 将 ClassName、类和文件分别在命名空间和路径中向上移动一级似乎已经解决了问题。也就是说,它现在位于命名空间 Project.WebUserControls 和路径 c:..\Project\WebUserControls\ 中,

不知道为什么会这样。路径名远低于 255 个字符。


第二天,又坏了……没有改变任何东西。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

赢得她心 2024-10-09 09:10:56

听起来 vstudio/IIS 所做的卷影副本没有被正确重建/使用。

尝试内置的开发服务器(在 vstudio 中)或重新启动 IIS

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

野の 2024-10-09 09:10:56

对我来说,听起来更像是 Visual Studio 问题,而不是 C#/.NET 问题。

只是为了澄清:只有在添加一些类然后尝试访问它之后才会出现这些错误,或者即使您没有添加任何内容,在每次构建之后也会随机出现这些错误?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文