ASP.NET Build - Bin Folder vs CodeBehind File - c#

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.

Related

ASMX dll not loaded when in subfolder

I am using ASP.NET (3.5) webforms. I have a plugin for a website that contains an .ascx file and an .asmx file. My folder structure looks like this:
MainWebSiteFolder
bin
Plugins
Plugin1
Plugin1.ascx
Plugin1.asmx
bin
Plugin1.dll
At runtime, the main website (using the web.config tag) can find the Plugin1.dll and load the Plugin1.ascx into the webpage. The Plugin1.ascx contains javascript code that calls the Plugin1.asmx, but IIS can't find the Plugin1.asmx codebehind because it's looking for Plugin1.dll in the main website's bin folder instead of the plugin's bin folder.
Trying to reach the asmx directly via URL (http://MyWebSite/Plugins/Plugin1/Plugin1.asmx) results in the following error:
RunTime Error
Could not create type 'MyNamespace.Plugin1'.
Line 1: <%# WebService Language="C#" CodeBehind="Plugin1.asmx.cs" Class="MyNamespace.Plugin1" %>
There are several similar questions out there, but none of them solve my problem because I just own the plugin, so I don't want to copy the plugin.dll into the main website's bin folder (because, when the main website is upgraded, its bin folder is emptied. I don't have control over this). I also don't want to make the asmx its own separate IIS application because it needs to be able to share a session with the main website.
Is there anything I can do to connect the asmx file with its dll inside the subfolder?
Check if adding a web.config to the Plugins or your Plugin1 directory (web.config inheritance), containing an assembly probing-element works (see probing Element on MSDN).

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

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

How do I place codebehind in multiple files?

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!

Categories