我的&QUOT的无;后面&QUOT code; code被调用 [英] None of my "code behind" code is being called

查看:239
本文介绍了我的&QUOT的无;后面&QUOT code; code被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have just created an ASP.NET C# project and a virtual directory for it in IIS in (as far as I know) the normal way, but I am seeing very strange behavior that I have never seen before.

似乎无我的C#方法是不断被调用。我知道这是因为我已经覆盖了一堆的方法做什么,但抛出一个异常。至少Default.aspx的是在浏览器上查看(见下文)

It seems that none of my C# methods are ever being called. I know this because I have overridden a bunch of methods to do nothing but throw an exception. At least Default.aspx is viewable in the browser (see below)

下面是我Default.aspx.cs文件的具体内容:

Here is the exact content of my Default.aspx.cs file:

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");
        }
    }

}

我想这code没有被加载在所有的,因为如果当时我会看到一个例外,每当我试图在浏览器中查看页面。而不是从.aspx文件中的内容通常出现(除了我的事件处理程序没有被调用。)

I assume this code is not being loaded at all, because if it was then I would see an exception whenever I tried to view the page in the browser. Instead the content from the .aspx file appears normally (except that my event handlers are not called.)

当我尝试添加新的.aspx页更糟糕。当我尝试在浏览器中查看一个新的页面(这是从VS2008模板未修改的cs文件)我得到这个错误:
分析器错误

It gets worse when I try to add new .aspx pages. I get this error when I try to view a new page in the browser (this is with the unmodified .cs file from the VS2008 template): Parser Error

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站点进入这个奇怪的状态?

What can cause an ASP.NET site to get into this strange state?

请注意:<%...%GT; 逃脱在.aspx文件仍然正常工作。此外,当我在.aspx文件中添加表单域,我可以在cs文件自动完成他们的名字。我曾经尝试都真正 AutoEventWireup 两个页面。我也尝试添加并从所有类声明去掉部分。

Note: <%...%> escapes in the .aspx file still work fine. Also when I add form fields in the .aspx file, I can auto-complete their names in the .cs file. I have tried both true and false for AutoEventWireup on both pages. I have also tried adding and removing "partial" from all class declarations.

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

Update - here are my @Page tags. As I said, I have tried toggling AutoEventWireup. The referenced .cs files exist and compile with no errors.

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

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


相关阅读:


Related:

  • CodeFile vs CodeBehind
  • ASP.NET: Code behind or no code behind?

推荐答案

有很多相互矛盾的信息在这里。例如,如果你是真正创建一个 ASP.NET Web应用程序(而不是一个网站),那么您不该使用 codeFILE ,作为<一个href=\"http://stackoverflow.com/questions/4879505/none-of-my-$c$c-behind-$c$c-is-being-called/4879669#4879669\">used2could建议。

There's a lot of conflicting information here. For example, if you are truly creating an ASP.NET Web Application (as opposed to a web site), then you should not be using CodeFile, as used2could suggests.

您是否尝试过检查的生成操作您的code隐藏文件的?请确保它被设置为编译

Have you tried checking the Build Action of your code-behind files? Make sure it is set to Compile.

我认为我们需要从头开始你,找出问题是否从你的网络的项目,您的IIS配置,或两者来了。

I think we need to start you from scratch, to identify if the problem is coming from your web project, your IIS configuration, or both.

我要做出你设置以下的假设,因为这是我的当前设置。让我知道,如果这些都是错误的,但它不应该产生巨大的变化:

I'm going to make the following assumptions about your set up, because this is my current set up. Let me know if any of these are wrong, but it shouldn't make a huge difference:


  • 您正在使用Visual Studio 2010与.NET 3.5

  • 您的Web服务器是Windows 2003

  • 您的Web服务器正在运行IIS 6.0

让我们尽量保持这个尽可能简单,尽量减少古怪的任何机会:

Let's try to keep this as simple as possible, to minimize any chance of weirdness:


  • 解决方案TestWebApp1

    • 项目TestWebApp1(ASP.NET Web应用程序)

      • 属性

      • 参考

      • 的App_Data
      • 脚本

      • Default.aspx的(生成操作:内容

        • Default.aspx.cs(生成操作:编译

        • Solution 'TestWebApp1'
          • Project 'TestWebApp1' (ASP.NET Web Application)
            • Properties
            • References
            • App_Data
            • Scripts
            • Default.aspx (Build Action: Content)
              • Default.aspx.cs (Build Action: Compile)

              • SiteLayout.Master.cs(生成操作:编译

              • SiteLayout.Master.cs (Build Action: Compile)

              内容的 Default.aspx的

              <%@ Page Title="" Language="C#" MasterPageFile="~/SiteLayout.Master"
                  AutoEventWireup="true" CodeBehind="Default.aspx.cs"
                  Inherits="TestWebApp1.Default" %>
              <asp:Content ID="Content2" ContentPlaceHolderID="mainCPH" runat="server">
                  <p><asp:Label ID="lblTest" runat="server">This is a test</asp:Label></p>
              </asp:Content>
              

              内容的 Default.aspx.cs

              using System;
              namespace TestWebApp1
              {
                  public partial class Default : System.Web.UI.Page
                  {
                      protected void Page_Load(object sender, EventArgs e)
                      {
                          lblTest.Text = "Modified from Default.aspx's Page_Load method.";
                      }
                  }
              }
              

              内容的 SiteLayout.Master

              <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="SiteLayout.master.cs"
                  Inherits="TestWebApp1.SiteLayout" %>
              <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
              <html xmlns="http://www.w3.org/1999/xhtml">
                  <body>
                      <form id="form1" runat="server">
                          <div>
                              <p><asp:Label ID="lblTest" runat="server">This is a test</asp:Label></p>
                              <asp:ContentPlaceHolder ID="mainCPH" runat="server">
                              </asp:ContentPlaceHolder>
                          </div>
                      </form>
                  </body>
              </html>
              

              内容的 SiteLayout.Master.cs

              using System;
              namespace TestWebApp1
              {
                  public partial class SiteLayout : System.Web.UI.MasterPage
                  {
                      protected void Page_Load(object sender, EventArgs e)
                      {
                          lblTest.Text = "Modified from master page's Page_Load method.";
                      }
                  }
              }
              

              现在,这个网站应该不会在本地计算机调试时失败:

              Now, this site should work without fail when debugging in your local computer:


              1. 右键单击 TestWebApp1 项目,然后单击发布。

              2. 选择File System作为发布方法为简单。

              3. 输入所在的文件将被部署的路径。

              4. 在您的Web服务器,打开IIS(我会假设你正在运行IIS 6.0)

              5. 默认Web站点(或任何你使用的网站),创建一个新的虚拟目录即可。确保它有权限来运行脚本。

              6. 复制从您的开发计算机发布到IIS虚拟目录中的文件。

              7. 这就是它 - 你的网站应该是工作的罚款

              1. Right click on TestWebApp1 project and click Publish.
              2. Choose File System as the 'Publish method' for simplicity.
              3. Enter a path where the files will be deployed.
              4. On your web server, open up IIS (I'm going to assume you're running IIS 6.0)
              5. Under Default Web Site (or whatever site you use), create a new Virtual Directory. Make sure it has permissions to run Scripts.
              6. Copy the files that were published from your dev machine to the IIS virtual directory.
              7. That's it -- your site should be working fine.


              完成以上步骤之后,你仍然得到问题?

              After following the above steps, are you still getting problems?

              这篇关于我的&QUOT的无;后面&QUOT code; code被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆