ASP.NET错误:页面Y.ascx无法使用用户控件X.ascx

时间:2022-09-02 12:23:01

I am getting the error below when trying to build the web site project in Visual Studio 2010:

尝试在Visual Studio 2010中构建网站项目时,我收到以下错误:

The page '/WebSite/controls/C2.ascx' cannot use the user control '/WebSite/controls/C1.ascx', because it is registered in web.config and lives in the same directory as the page.

页面'/WebSite/controls/C2.ascx'不能使用用户控件'/WebSite/controls/C1.ascx',因为它在web.config中注册并与页面位于同一目录中。

I have 2 web user controls:

我有2个Web用户控件:

controls/C1.ascx
controls/C2.ascx

The controls have been registered in web.config:

控件已在web.config中注册:

<configuration>
    <system.web>
        <pages>
            <controls>
                <add src="~/controls/C1.ascx" tagPrefix="my" tagName="C1"/>
                <add src="~/controls/C2.ascx" tagPrefix="my" tagName="C2"/>
            </controls>
        </pages>
    </system.web>
</configuration>

C1.ascx contains just a static HTML, C2.ascx is trying to include C1:

C1.ascx只包含一个静态HTML,C2.ascx试图包含C1:

C1.ascx contains just some plain static simple HTML. C2.ascx is trying to include C1.ascx:

C1.ascx只包含一些简单的静态简单HTML。 C2.ascx试图包含C1.ascx:

<%@ Control Language="VB" %>
<my:C1 runat="server" />
<p>Hello from C2</p>

When trying to build the project, I am getting the error message at the top. I realise this issue can be fixed by adding another Register directive to C2.ascx...:

在尝试构建项目时,我收到顶部的错误消息。我意识到可以通过向C2.ascx添加另一个Register指令来修复此问题...:

<%@ Register Src="~/controls/C1.ascx" TagPrefix="ctl" TagName="C1" %>

...but I'm wondering if there's a cleaner solution and why am I getting the error in the first place?

...但我想知道是否有更清洁的解决方案,为什么我首先得到错误?

Thanks!

谢谢!

2 个解决方案

#1


25  

Your only possible solutions are to:

您唯一可行的解​​决方案是:

  • Move the control out of the directory its currently sharing with outer.ascx, or
  • 将控件移出当前与outer.ascx共享的目录,或者
  • Re-register the control inside of the outer.ascx like you already mentioned
  • 像你刚才提到的那样在outer.ascx中重新注册控件
  • Re-write them in code as controls in a separate library
  • 将它们作为控件重新写入单独的库中

I personally think moving is the easiest, if it will work for your solutions. Second would be re-registering, even though annoying. Abstracting them out into a full code library is probably not worth the effort if this is the only reason you are doing it.

我个人认为移动是最简单的,如果它适用于您的解决方案。第二个是重新注册,即使令人讨厌。如果这是你做这件事的唯一原因,那么将它们抽象成一个完整的代码库可能是不值得的。

#2


5  

You could also put the controls into different folders. But I don't think this is much cleaner or better.

您还可以将控件放入不同的文件夹中。但我认为这不是更清洁或更好。

BTW: this behavior is by design, as you can read on this MSDN page (look for the yellow note almost at the end of the page).

顺便说一句:这种行为是设计使然,因为你可以在这个MSDN页面上阅读(几乎在页面末尾查找黄色注释)。

#1


25  

Your only possible solutions are to:

您唯一可行的解​​决方案是:

  • Move the control out of the directory its currently sharing with outer.ascx, or
  • 将控件移出当前与outer.ascx共享的目录,或者
  • Re-register the control inside of the outer.ascx like you already mentioned
  • 像你刚才提到的那样在outer.ascx中重新注册控件
  • Re-write them in code as controls in a separate library
  • 将它们作为控件重新写入单独的库中

I personally think moving is the easiest, if it will work for your solutions. Second would be re-registering, even though annoying. Abstracting them out into a full code library is probably not worth the effort if this is the only reason you are doing it.

我个人认为移动是最简单的,如果它适用于您的解决方案。第二个是重新注册,即使令人讨厌。如果这是你做这件事的唯一原因,那么将它们抽象成一个完整的代码库可能是不值得的。

#2


5  

You could also put the controls into different folders. But I don't think this is much cleaner or better.

您还可以将控件放入不同的文件夹中。但我认为这不是更清洁或更好。

BTW: this behavior is by design, as you can read on this MSDN page (look for the yellow note almost at the end of the page).

顺便说一句:这种行为是设计使然,因为你可以在这个MSDN页面上阅读(几乎在页面末尾查找黄色注释)。