在已部署的 Web 应用程序中出现编译错误,但在 IDE 中却没有出现编译错误

发布于 2024-10-09 22:39:15 字数 1801 浏览 0 评论 0原文

在我部署的 Web 应用程序中,出现以下错误:

CS0103:名称“releaseLionButton_Click”确实 当前上下文中不存在

它将此标识为有问题的行:

<asp:Button ID="releaseLionButton" runat="server" 
            Text="Release the Lion!"
            OnClick="releaseLionButton_Click"></asp:Button>

在代码隐藏文件中,我有以下代码:

internal void releaseLionButton_Click(object sender, EventArgs e)
{
    if (selectedLionCage != null && selectedLionCage.DoorState == DoorState.Closed)
        selectedLionCage.OpenDoor();
}

现在我可以通过将 internal 更改为 protected 来消除此错误,这是有道理的,因为 ASPX 页面表示它从代码隐藏文件中定义的 Page 类“Interits”。

没关系。

我不明白的是,当我在本地计算机上的 IDE 中运行该程序时,为什么该程序与 internal 一起工作。

我不想让测试人员遇到一百个这样的愚蠢错误,我也不想为一百个略有不同的程序版本做测试部署文书工作。

如果它无法真正工作,我希望它在 IDE 中无法工作

所以这是我的问题:

如何在 IDE 中获取本地版本来生成部署版本将遇到的所有编译错误?

更多信息可分散您对真正问题的注意力:

应用程序的目标框架是“.NET Framework 3.5”。一定是这个,因为它引用了 System.DirectoryServices.AccountManagement。

相比之下,测试机上的错误页面显示“版本信息:Microsoft .NET Framework 版本:2.0.50727.4206;ASP.NET 版本:2.0.50727.4209”。

然而目录服务帐户管理代码在测试机器上运行得很好。我也不知道那里发生了什么。

我遇到的其他类似问题

这是我正在维护的一个较旧的应用程序,它偶尔会为我提供一些随机 ASPX 文件的一长串验证警告。即使代码仍然有效,我也修复了所有这些问题,因为我喜欢我的代码干净漂亮。我还没有找到一种方法来专门为所有文件或任何特定文件生成验证警告。

有时,当我重构代码(例如通过更改变量名称)时,它会警告我该代码当前未编译,因此重构可能不起作用。但我无法让 IDE 告诉我为什么它认为代码无法编译。它仍然运行完美,并且错误列表中没有出现任何错误或警告!其他时候,由于我没有发现任何模式,它在重构期间不会显示警告消息。

有关 ASPX 文件验证的更新

回答时另一个问题有人向我指出,如果为 C# 编辑器启用了“显示实时语义错误”选项,则对 ASPX 文件的验证将起作用。这解释了为什么我遇到了其他人似乎没有遇到的问题。

In my deployed web application I am getting the following error:

CS0103: The name 'releaseLionButton_Click' does
not exist in the current context

It identifies this as the offending line:

<asp:Button ID="releaseLionButton" runat="server" 
            Text="Release the Lion!"
            OnClick="releaseLionButton_Click"></asp:Button>

In the code-behind file, I have the following code:

internal void releaseLionButton_Click(object sender, EventArgs e)
{
    if (selectedLionCage != null && selectedLionCage.DoorState == DoorState.Closed)
        selectedLionCage.OpenDoor();
}

Now I can make this error go away by changing internal to protected, which makes sense, since the ASPX page says it "Interits" from the Page class defined in the code-behind file.

That's all fine.

What I don't understand is why the program works with internal when I run the program in the IDE on my local machine.

I don't want the testers to run into a hundred such stupid errors, and I don't want to do the test deployment paperwork for a hundred slightly-different versions of the program.

I want it to not work in the IDE if it's not going to work for real!

So here's my question:

How can I get the local version in the IDE to generate all of the compilation errors that the deployed version is going to experience?

More information to distract you from the real problem:

The Target Framework for the application is ".NET Framework 3.5". It must be this because it references System.DirectoryServices.AccountManagement.

The error page on the test machine, in contrast, says "Version Information: Microsoft .NET Framework Version:2.0.50727.4206; ASP.NET Version:2.0.50727.4209".

And yet the Directory Services Account Management code works perfectly on the test machine. I don't know what's going on there either.

Other, similar problems that I am experiencing

This is an older application that I am maintaining, and it will occasionally give me a long list of validation warnings for some random ASPX file. I fix all of these even though the code still works, because I like my code to be clean and pretty. I have not yet found a way to generate the validation warnings on purpose for all files or for any particular file.

Sometimes when I refactor the code (such as by changing a variable name) it will warn me that the code does not currently compile, so the refactoring may not work. And yet I cannot get the IDE to tell me why it thinks the code won't compile. It still runs perfectly, and no errors or warnings appear in the Error List! Other times, with no pattern that I have discovered, it will not display the warning message during a refactoring.

UPDATE regarding the validation of ASPX files

When answering another question it was pointed out to me that validation for ASPX files will work if the "Show live semantic errors" option is enabled for the C# editor. This explains why I was having problems that nobody else seemed to be having.

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

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

发布评论

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

评论(2

我的鱼塘能养鲲 2024-10-16 22:39:15

问题是,如果您有一个带有 .aspx 和 .cs/vb 代码隐藏的传统 asp.net 网站,则实际上涉及两个不同的程序集。您的代码隐藏编译为一个程序集,IIS 将获取 .aspx 并将其动态编译为另一程序集。当您将某些内容标记为内部时,您定义了程序集边界,因此动态 aspx 程序集无法引用代码隐藏程序集中的内部成员。

现在,至于为什么它不能像 VS 的网络服务器那样工作,我不知道,因为我不熟悉它的细节。但是,您可以做的只是在计算机上的 IIS 中设置您的网站。这样它的行为就会与“生产”版本完全相同。

如果您担心调试它,那没问题,使用管理权限运行 Visual Studio,当您的计算机上发生服务器错误时,使用 Visual Studio 附加到 w3wp.exe,它会找出问题并在错误。

The problem is, if you have a traditional asp.net website with .aspx and .cs/vb codebehinds, is that there are actually two different assemblies involved. Your codebehind compiles to one assembly, and IIS will take the .aspx and compile it dynamically to another assembly. When you mark something as internal you define an assembly boundary, and thus the dynamic aspx assembly can't reference the internal member in the codebehind assembly.

Now, as to why it doesn't work like that in VS's webserver I don't know as I'm not familiar with the particulars of it. However, what you can do is just set up your website in IIS on your machine. That way it will behave exactly like the "production" version.

If you're worried about debugging it, it's no problem, run Visual Studio with administrative rights, and when a server error on your machine occurs, attach to the w3wp.exe using Visual Studio and it'll figure it out and break on the error.

晒暮凉 2024-10-16 22:39:15

我猜您没有在解决方案中编译您的网络项目。

I would guess you are not compiling your web projects in your solution.

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