c# - 我的 "code behind"代码都没有被调用

标签 c# asp.net iis asp.net-3.5

我刚刚在 IIS 中以正常方式(据我所知)创建了一个 ASP.NET C# 项目和一个虚拟目录,但我看到了我以前从未见过的非常奇怪的行为。

似乎没有我的 C# 方法被调用过。我知道这一点是因为我重写了一堆方法,除了抛出异常外什么都不做。至少 Default.aspx 在浏览器中是可见的(见下文)

这是我的 Default.aspx.cs 文件的确切内容:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Drawings2
{
    public partial class _Default : System.Web.UI.Page
    {
        static _Default()
        {
            throw new Exception("XXX");
        }
        public _Default()
        {
            throw new Exception("XXX");
        }
        override protected void OnInit(EventArgs e)
        {
            /*
             * base.OnInit(e);
             * InitializeComponent();
             */
            throw new Exception("XXX");
        }
        private void InitializeComponent()
        {
            /*
             * Load += new EventHandler(this.Page_Load);
             */
            throw new Exception("XXX");
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            throw new Exception("XXX");
        }
    }

}

我假设这段代码根本没有被加载,因为如果加载了,那么每当我尝试在浏览器中查看页面时都会看到异常。相反,.aspx 文件中的内容正常显示(除了未调用我的事件处理程序。)

当我尝试添加新的 .aspx 页面时,情况变得更糟。当我尝试在浏览器中查看新页面时出现此错误(这是来自 VS2008 模板的未修改的 .cs 文件): 解析器错误

Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately. 

Parser Error Message: Could not load type 'Drawings2.WebForm1'.

什么会导致 ASP.NET 站点进入这种奇怪的状态?

备注:<%...%> .aspx 文件中的转义仍然可以正常工作。此外,当我在 .aspx 文件中添加表单字段时,我可以在 .cs 文件中自动完成它们的名称。我都试过了 truefalse对于 AutoEventWireup在两个页面上。我还尝试在所有类声明中添加和删除“部分”。


更新 - 这是我的@Page 标签。正如我所说,我尝试切换 AutoEventWireup .引用.cs文件存在并且编译没有错误。

<%@ Page Language="C#" AutoEventWireup="false" CodeBehind="Default.aspx.cs" Inherits="Drawings2._Default" %>

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Drawings2.WebForm1" %>

相关:

最佳答案

尝试改变:

<%@ Page Language="C#" AutoEventWireup="false" CodeBehind="Default.aspx.cs" Inherits="Drawings2._Default" %>

到:

<%@ Page Language="C#" AutoEventWireup="false" CodeFile="Default.aspx.cs" Inherits="Drawings2._Default" %>

CodeBehind 用于 visual studio。我相信 CodeFile 用于 JIT。

另一种选择是编译您的项目并在 bin 目录中更新您的程序集。

http://msdn.microsoft.com/en-us/library/ydy4x04a.aspx

CodeBehind Specifies the name of the compiled file that contains the class associated with the page. This attribute is not used at run time.

This attribute is used for Web application projects. The CodeFile attribute is used for Web site projects. For more information about Web project types in Visual Studio, see Web Application Projects versus Web Site Projects.

CodeFile Specifies a path to the referenced code-behind file for the page. This attribute is used together with the Inherits attribute to associate a code-behind source file with a Web page. The attribute is valid only for compiled pages.

This attribute is used for Web site projects.

The CodeBehind attribute is used for Web application projects. For more information about Web project types in Visual Studio, see Web Application Projects versus Web Site Projects.

关于c# - 我的 "code behind"代码都没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4879505/

相关文章:

c# - 如果 timeInterval 没有用户交互,如何恢复到页面

iis - 如何复制网络场上的内容

c# - 多线程:限制并发线程数

c# - MVVM Light 在哪里处理 api

asp.net - DropdownList 重置在更新面板的提交按钮上不起作用

node.js - 是否有可能在 Azure 上获得 IIS 的免费 ssl 证书

asp.net - 无法部署到 Elastic Beanstalk : ERROR_FILE_IN_USE

c# - 从调用的 WebMethod 更新 UpdatePanel

c# - 使用 FileSystemWatcher 检测文件夹重命名

javascript - div html不包含ajax帖子中的输入值