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
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.
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:
Let's try to keep this as simple as possible, to minimize any chance of weirdness:
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:
After following the above steps, are you still getting problems?
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:
"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?
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!
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
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.