None of my “code behind” code is being called

前端 未结 9 1908
予麋鹿
予麋鹿 2021-02-08 00:40

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 see

9条回答
  •  失恋的感觉
    2021-02-08 01:03

    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.

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

    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:

    • You're using Visual Studio 2010 with .NET 3.5
    • Your web server is Windows 2003
    • Your web server is running IIS 6.0

    Creating a new web app project:

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

    • 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 (Build Action: Content)
          • SiteLayout.Master.cs (Build Action: Compile)
        • Web.config

    Contents of Default.aspx:

    <%@ Page Title="" Language="C#" MasterPageFile="~/SiteLayout.Master"
        AutoEventWireup="true" CodeBehind="Default.aspx.cs"
        Inherits="TestWebApp1.Default" %>
    
        

    This is a test

    Contents of 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.";
            }
        }
    }
    

    Contents of SiteLayout.Master:

    <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="SiteLayout.master.cs"
        Inherits="TestWebApp1.SiteLayout" %>
    
    
        
            

    This is a test

    Contents of 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:

    site working locally

    Deploying to IIS

    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.

    Basic VS 2010 Publish Dialog Virtual directory Access Permissions

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

提交回复
热议问题