12

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.

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)

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

}

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.)

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'.

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

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.


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:

Community
  • 1
  • 1
finnw
  • 47,861
  • 24
  • 143
  • 221
  • 1
    What does the `@Page` tag look like on your aspx? –  Feb 02 '11 at 20:31
  • This may be a silly question, but is .NET registered with IIS? Maybe try `aspnet_regiis -i`? – Jason Towne Feb 02 '11 at 21:03
  • @Jason Towne, I will check but I assume so because another ASP.NET app on this server works fine. – finnw Feb 02 '11 at 21:05
  • @finnw, Is this an issue again? Why the bounty if there's already an accepted answer? – Jason Towne Mar 14 '11 at 20:07
  • @Jason I'm about to edit the question to explain the remaining issue – finnw Mar 14 '11 at 20:09
  • Can you upload the project somewhere so that we can have a look? – Mikael Östberg Mar 14 '11 at 23:24
  • I'm with @MikeEast. It almost sounds as if you have both a Web Site project and a Web Application project where the Web Site project is referencing the Web Application. – Brian Mar 16 '11 at 05:54
  • Can you rename your MasterPage class to something like DefaultTestXX and provide the error even if it seems like the same error? – Brian Mar 16 '11 at 05:56
  • If you still can't get it working then hit me up @ facebook/used2could and we can do a screen share or something. – Brian Mar 16 '11 at 06:06

9 Answers9

13

Try changing :

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

to:

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

The CodeBehind is for visual studio. I believe CodeFile is used for the JIT.

The other alternative is to compile your project and update your assembly in the bin dir.

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.

Brian
  • 4,974
  • 2
  • 28
  • 30
  • Np! good luck. This has tripped me up more than a couple of times when switching between a Web App and a Web Site project. – Brian Feb 02 '11 at 21:12
  • has the eagle eye. I never would have spotted that. –  Feb 02 '11 at 21:35
  • @yodaj007: It has stolen a few hours from me, i'll never be able to get them back. lol – Brian Feb 03 '11 at 15:42
  • Unfortunately this introduces a new error (see the update to the question.) – finnw Mar 14 '11 at 20:19
  • Unaccepting. This temporarily fixed one bug, but caused a worse one later on so I cannot recommend it. – finnw Mar 21 '11 at 15:28
4

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" %>
<asp:Content ID="Content2" ContentPlaceHolderID="mainCPH" runat="server">
    <p><asp:Label ID="lblTest" runat="server">This is a test</asp:Label></p>
</asp:Content>

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" %>
<!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>

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?

Community
  • 1
  • 1
Pandincus
  • 9,506
  • 9
  • 43
  • 61
  • I was hoping to diagnose the root cause of the problem and fix it in the existing project rather than create a new one. However this is a great example and it solves the problem (since my code works if I copy it into your project.) Also thanks for pointing out the `CodeFile` & build action gotchas. – finnw Mar 21 '11 at 15:27
  • @finnw - Sorry it didn't help to identify the root cause. My thinking was that after trying various solutions, the project was probably fubar'd anyway ;-). Glad the 'new project' solution worked, though! – Pandincus Mar 21 '11 at 17:21
  • 1
    @finnw: Out of curiosity, is there any reason in particular you decided to accept this answer but give the bounty to @Jason Kealey? – BlueRaja - Danny Pflughoeft Mar 21 '11 at 17:26
2

I learned this the hard way and it might be your issue.

Make sure the bin folder is in the root of the website in IIS.

I had www.mysite.com/myapp/bin and that didn't work, I needed to have the dlls in www.mysite.com/bin, even if my pages were all inside www.mysite.com/myapp (In my case I had Wordpress running in the root with PHP and ASP.NET stuff in a subfolder for certain pages)

(What made it worse to track down for me is that there was a bin folder in the root already from a previous deploy and I was updating the site and wondering why my code-behind changes weren't taking effect)

EDIT: Found out how to avoid putting the bin folder in the website root. In IIS, if you right-click on your application folder in the website, there's an option to "Convert to application" (at least there is in IIS 7.5 not sure about older versions). After doing that, that folder will be able to use it's own bin and web.config

EDIT2: Instructions for IIS 6 and 7: http://www.banmanpro.com/support2/appstartpoint.asp

Davy8
  • 30,868
  • 25
  • 115
  • 173
  • I hope this is not the only solution, because I am unable to do this (the customer requires my app to be in a subdirectory - they won't permit any of my code in `~` or `~/bin`.) – finnw Mar 14 '11 at 20:35
  • @finnw see my edit. I thought it was strange that I had to do that as well, so it prompted me to try to find another solution. Let me know if the above works for you. – Davy8 Mar 14 '11 at 21:12
  • @finnw Did you get a chance to see if my updated answer works for you? – Davy8 Mar 15 '11 at 16:19
1

I suspect your Page declaration (<%@ Page ... %>) in the apsx file is wrong. Look at ClassName and CodeBehind attributes and make sure they match your .cs file.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
1

I think IIS thinks your project is a 'ASP.NET site' whereas you and/or your VS.NET may think that you actually have a 'ASP.NET Web Application'. Hitting convert to web application on the project within VS.NET and ensuring you have a properly configured (see other comments) virtual directory on project in IIS, everything should work fine.

Reading your post & comments, I see contradictory comments (unless I am misreading)

You say:

  1. "Work with the "ASP.NET Web Application" template (vs. ASP.NET Site)".
  2. "I hope this is not the only solution, because I am unable to do this (the customer requires my app to be in a subdirectory - they won't permit any of my code in ~ or ~/bin.)"

"ASP.NET Web Application" requires compiled dlls when deployed, not the base .cs files.

Is the root cause conceptual confusion between what you want to use as a dev and what you are allowed to deploy by the customer?

Jason Kealey
  • 7,988
  • 11
  • 42
  • 55
  • *"ASP.NET Web Application" requires compiled dlls when deployed, not the base .cs files.* - that appears to be the answer to the second problem. I was deploying the .cs files to the server and though I would expect IIS to ignore them, it seems they confuse it. – finnw Mar 21 '11 at 15:40
1

I get the runtime error on a semi-regular basis; clearing out:

C:\Documents and Settings\\Local Settings\Application Data\Microsoft\WebsiteCache C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files

(obviously substitute your ) and running iisreset usually does the job for me. Also, try 'cleaning' the solution before you rebuild; sometimes appears to make a difference.

If you're still getting the same problem, try dropping and recreating the reference to the DLL.

Nothing too prescriptive I'm afraid but should hopefully get the job done!

Dave
  • 3,581
  • 1
  • 22
  • 25
1

This appears to be a namespace issue as the dynamically compiled classes are colliding with the pre-compiled classes. I suggest doing the following:

1) Select a reasonable namespace for your app. "Drawing" is fine, but I suggest something more contextual such as CompanyName.ProjectName.AppName

2) Set this namespace as the default for the project (Project Properties > Application tab)

3) Change the namespace in each and every code file (.cs) and its corresponding reference in script files (.aspx, *.ascx)

4) Ensure that there are no duplicate classes (this should be flagged by the compiler)

5) Purge all files from the temporary ASP.NET folder

6) Recompile the project

Nik Kalyani
  • 916
  • 5
  • 10
  • I have a hunch that you're on the right track, but none of these suggestions work (the error still occurs with a brand-new class in a brand-new namespace.) – finnw Mar 19 '11 at 22:30
  • Here's a link to a working VS2010 solution that contains a project in the Company.AppName.Example namespace that was created with the ASP.Net Web Application Template. The code-behind for Default.aspx writes a string value to the page each time an event is called. This is for ASP.NET 3.5, but will work fine with 4.0. http://dl.dropbox.com/u/17121319/Example.zip – Nik Kalyani Mar 20 '11 at 01:10
0

I think I may have encountered something similar before. It turned out that it was because I had a default.aspx and a default.master which both auto compiles to the class name _default, therefore getting type exists in two dlls error.

kim
  • 71
  • 9
  • 1
    I have heard of this, but it is not the case in my app (in any case I tried renaming classes in case the problem was stale compiled versions.) – finnw Mar 18 '11 at 05:38
0

Remove the following page directive from your HTML view, build the website and try to load the page in the browser.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Drawings2.WebForm1" %>
Billy Samuel
  • 672
  • 2
  • 6
  • 21