How do I place codebehind in multiple files? - c#

I'm running in to a lot of inconsistencies with regards to what will compile and execute correctly in a Visual Web Developer 2008 Express environment and what fails on my web server.
In particular, I've got an aspx and aspx.cs codebehind, plus a number of additional .cs files in my Web Developer project. It builds fine and executes okay under the Development Server.
Once I upload the files to my server, the codebehind doesn't seem to be aware of the other .cs files.
What's the correct way to make my aspx app inherit additional .cs files?
--- Update ---
Since I'm not really finding the answer I need, let me be a little more explicit with what I'm doing:
I have three files:
Default.aspx
<%# Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%# Import Namespace="UtilClasses" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Default.aspx.cs
public partial class _Default : Page { }
App_Code/UtilClasses.cs
namespace UtilClasses {
public class AClass {
public const int A = 1;
}
}
In this example, if I attempt to do any of the following, I'll get a compilation error on my web server:
Reference the App_Code with a #Import
Call the code contained in it from the aspx.cs codebehind or
Call the code from the aspx page.
It compiles fine in Web Developer 2008. The soltuion is a Web Site which is auto published to my web server via FTP. The exact list of files being published are:
Default.aspx
Default.aspx.cs
App_Code/UtilClasses.cs
web.config

It seems like you have a deployment problem.
Just publish (in build menu there is a publish item) your web application/site to a folder and then move the files in that folder to your server.
I think you have old assembly files and new classes in your directory.
hope this helps

You can use partial classes. Add a file ( *.cs) and define your class as partial. So that, you can distribute your methods,properties anaother files

This is a very common problem of organizing code in your web applications. Here are some pointers to help you attend to your problem.
If using a single project and it being ASP.net website project, put your .cs files in the app_code folder.
Use namespaces wisely and keep track of what is in which namespace. If u have something like Resharper it saves this task.
Ideally you should have only page related and util classes in your web project. Create libraries using C# express for other functions. Test these libraries using NUnit/MbUnit or the likes.
If you have doubts if something runs on Cassini but not in IIS. A dev or local IIS is the only solution.
Hope that helps!

Related

Why am Igetting the error "Could not load type 'CADE.main.index'." on a new server when I'm using the same code on all other instances, how fix it?

I just checked out my "mixed" (asp classic and .Net) web site source from SVN on a new Windows 2019 Server. When I go to the logon page (.asp classic), it's fine but when you logon and call the main page (.aspx) it throws the error: "Could not load type 'CADE.main.index'" and displays:
Line 1: <%# Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="CADE.main.index" %>
This SVN code is running on 15+ servers, including one 2019 Server. The code behind looks like:
namespace CADE.main
{
public partial class index : BasePage // Override with BasePage
{
IIS and everything under Application Development is installed. IIS serves .asp, .txt, .gif, .png, even .Net handlers (.ashx) and web services (.asmx), just not the aspx pages.
So, I rebuilt the solution with VS2019 and published it, gave administrator permissions on the app pool, gave "Everyone" full permissions on the entire web, deleted the bin directories in both the source and web locations, ensured output path was bin/, cleaned the project, don't think I should change CodeBehind to CodeFile, and tried about every other thing that people have suggested on this web site but nothing has eliminated the error.
Question: How do I fix this error so the .aspx pages loads?
EDIT #1: 15 hours after post
I'm not sure but with this information maybe this should be a new post.
This error ONLY happens on the second web site. IIS is serving two web sites, separating them by host name (web1.com vs. web2.com) in bindings. They both point at the same file structure. Web1's subdirectories are natural while Web2's subdirectories are virtual directories that point to Web1's.
Web1: Normal IIS web
wwwroot
LMW (subweb, LMW/Main/index.aspx is page in question, but works here)
Web2: Second web in IIS, based on host name, uses Web1's folders as virtual directories.
wwwroot
WEB2 (folder under wwwroot)
LMW (virtual directory points to Web1/wwwroot/LMW. LMW/Main/index.aspx gives the error here)
Question: Why doesn't the .aspx work when it's a virtual directory?... it's the same file as is in Web 1?
EDIT #2
It is working but I don't know what I did to fix it. I'm sure I changed something but I don't know what it was. So, I created another web like web #2, New web folder under wwwroot with LMW a virtual directory pointing to Web1/wwwroot/LMW! I'm seriously missing something, Any ideas?
These errors occur if the .aspx page or the Global.asax page contains a reference to a code-behind module and if the application has not been built.
You could try the below method to build the application:
Use the C# command-line compiler (CSC.exe) to run the following command:
csc /t:library /r:System.web.dll /out:mydll.dll myfile.cs
OR
In Microsoft Visual Studio .NET, click Build on the Build menu.
Also try changing CodeBehind="index.aspx.cs" to CodeFile="index.aspx.cs". This change is required when you change project type from website to webapplication or vice versa.
make sure you install the .ne framework feature of iis.
I actually created a brand new web that used a Virtual Directory for it's codebase that is the same code as the other webs.
Index.aspx resides in the /lmw/main directory. I had created an Application on that folder when I was creating the other applications in the web. So, I removed the "Application" from /lmw/main and it worked.

View ascx.cs files from deployed website?

I've got a website that I published on IIS, but it seems like I've got the wrong version of source code in my dev environment.
I just need the code from 1 ascx.cs file.
Can I get to the contents of an ascx.cs file from a deployed website?
I've tried using .NET Reflector as suggested on other posts, but it seems like this tool only gives me access to my normal classes and not to the .ascx.cs files. Or am I just missing that?
EDIT:
Additional information as requested in answers.
<%# Control Language="C#" AutoEventWireup="true" CodeFile="timesheet.ascx.cs" Inherits="controls_timesheet" %>
You should use the DLL of the compiled web site to get the class that represents your .ascx.cs file.
For example, if your project is called MyWebSiteProject, you should have a MyWebSiteProject.dll on your /bin folder.
Open this DLL with .net Reflector, or http://ilspy.net/, if your control is named MyUserControl, it should be inside the namespace MyWebSiteProject.
Please double check your project's and controls namespace first.
Hope this helps, good luck :)

ASP.NET Build - Bin Folder vs CodeBehind File

Assume there is a asp.net 4.0 web application and it has a default.aspx and default.aspx.cs files in it. After I build the project, a dll that is named of the project created in the bin folder. So what the dll contains ? All code behind files compiled versions ?
If the aspx files still refers its CodeBehind file like below, then does the dll used for this aspx file or still code behind is valid to run the project ?
<%# Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication3._Default" %>
In a Web Application, all your C# code is contained within the DLLs in the bin directory. There are a couple of exceptions, such as DLLs that you rely on that live in the GAC, for example. Using a web application ( your question says this is what you are using ), you do not need to deploy your *.cs code behind files.
A Web Site is different. Changes are detected and recompiled on the fly. You'll need to include your C# files code-behind files when creating a Web Site type project.
ASP.NET Web Site or ASP.NET Web Application?
A DLL is a library that contains code and data that can be used by more than one program at the same time:
What is a DLL?
Code-behind refers to code for your ASP.NET page that is contained within a separate class file. This allows a clean separation of your HTML from your presentation logic:
ASP.NET Code-Behind Model Overview
The code gets compiled into the assembly produced by your web project. You can change its name and default namespace as part of project options.
As I understood it, the ASPX file is only used IF the project is marked as being updatable, otherwise it is just a placeholder file.

could not load type <MyNameSpace>.Global

I'm currently working on an ASP.NET 4.0 site using a project-less solution.
By default the global.asax does not have a code-behind file, but after I changed it to
<%# Application Language="C#" CodeBehind="Global.asax.cs" Inherits="MyNamespace.Global" %>
And created an empty Global.asax.cs with the same namespace / class name I'm getting this error at compile time
Error 1 Could not load type 'MyNamespace.Global'. C:\Projects\RiskOptix\Code\RiskOptix.WebApplication\RiskOptix.WebApp\Global.asax 1
I've already tried cleaning out my entire bin folder but to no avail - this is extremely infuriating.
This question has already been asked. Check out this answer. Web site projects work differently from web application projects. Website type projects do not have CodeBehind files instead have CodeFile.
<%# Application CodeFile="Global.asax.cs" Inherits="MyNamespace.Global" Language="C#" %>
CodeBehind = Needs to be compiled ( asp.net 1.1 model) and compiled binary is placed in the bin folder of the website. You need to do a compile in visual studio before you deploy. Good model when you do not want the source code to be viewable as plain text ... for example when delivering to a customer who you not have obligation to provide code.
CodeFile = You provide the source file with the solution for deployment. ASP.NET 2.0 runtime compiles the code when needed. The compiled files are at Microsfot.NET[.NET version]\Temporary ASP.NET Files.
I was not able to run my MVC Web application. It was giving Could not load type <application namespace.Classname> error in Global.asax
I went to Project Properties and set the build>output folder to bin/ which was bin/Debug. Ran it once. It ran fine. And then again set the output folder to bin/Debug. Working fine now.
just to add my 2 cents with a WTF moment, My version of this error was caused by the Global.asax.cs not being included in the Visual Studio Project.
Right clicked on the .cs file, include in project and voila...
HTH
Dave
Yet another way to get this into this problem...
I had my web app open in VS2010, and IIS Express could run it just fine. Later, I opened the same web app, but in a newer branch with VS2012, and the virtual dirs in IIS Express auto-magically updated themselves to the physical dirs for the VS2012 project, without warning. So when I hit F5 to run my web app in VS2010 where I'm debugging, then I got the "could not load type or namespace" error in the global.asax file on one Import Namespace line. Closing both VS instances and reopening just the VS2010 version fixed the problem.

How to specify physical path in ASPX page?

I am developing a C# VS 2008 / SQL Server 2008 ASP.NET Web Applications project. In one of my ASPX files I am trying to reference the Master file, which is actually located in the parent website. In other words, when I open the parent website, I see this project listed. But when I open this project separately, I do not see parent website and this project is the root.
So now how do I use the Master file from the parent website? Currently, I have in my ASPX file:
<%# Page Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeFile="EnhancedCreateUserWizard.aspx.cs"
Inherits="Membership_EnhancedCreateUserWizard" Title="Untitled Page" %>
But this won't work because it is a virtual path and since this project is the root, I can't access the Master file virtually. Instead I want to specify physical path. How accomplish I do this?
You cannot share a master page between applications this way. All paths within an asp.net app are relative.
The virtual path provider simply will not reach above the current apps root.
You will need to either write a custom virtual path provider, which is not trivial, or package the master pages in a shared assembly.
You will find guidance in this question: What is the best way to share MasterPages across projects

Categories