Page class outside of App_Code will not compile - asp.net

I have a website that has 2 files as follows:
page.aspx
page.aspx.cs
It used to be that I could just drop new files onto the web server and IIS would automatically compile the files and I could access the page e.g.
http://www.website.com/page.aspx
... and the associated functionality in the page class contained in the .cs file would work nicely.
Now I get the error: "Could not load type namespace.classname" which refers to my page class.
Now for some strange reason I have to put all my .cs files, even page classes into the app_code folder.
All that has changed on my website is that I reorganised the structure so that instead of my pages being on the web root they are now inside http://.../newfolder/page.aspx.
For some reason all my page.aspx.cs files now have to be in app_code.
Any ideas?

Sounds like you are mixing up a Web Application Project and a Web Site.
Are you sure the files are exactly the same? Perhaps one #Page directive says CodeBehind=Page.aspx.cs and the other says CodeFile=Page.aspx.cs?
CodeBehind requires project compilation, so you cannot just drop in a new .cs file, you need to upload a new compiled DLL. CodeFile will allow dynamic compilation.
The App_Code directory is dynamically compiled (in both cases) when your app is accessed, so the Inherit directive has a valid type when you put the file there. In general, don't do this. You want the .cs file to go with the .aspx file. Use App_Code for business logic or utility classes that aren't associated with a particular page.
Finally, is this new subdirectory set up as a new app in IIS? What does the web.config file in your new directory change? Are you running the same version of ASP.NET? Check the "compilation" tag. I'm not sure what you could do there to cause this, but I'm sure you could cause some chaos.

Related

Does visual studio includes only aspx.cs files in dll which are having same namespace of project?

I am using visual studio 2012. I have created a directory named "Handler" inside my project and added an aspx file along with .cs file (both downloaded from a third party tool) to it. All code is working fine.
Now, I have published project to another directory. In published directory I have marked that the in side "Handler" folder aspx page is there but not the .cs page.
Now, when ever I am requesting the aspx page inside "Handler", I am getting error 404 xxxx.aspx page not found. Now, if I am coping the path and hit the url for that page then I am getting error that xxxx.aspx.cs file not found.
As per my best knowledge, whenever we build the project, all the code will build to dll in bin folder. Then why this file is required .cs file?
Thanks.
This will depend on the attributes of the #Page directive of your aspx page
I guess your third party page #page directive holds a CodeFile attribute.
CodeFile attribute states that, even though the .aspx.cs gets compiled (let's say it just gets checked) in Visual Studio, the result of the compilation will not be put in the dll resulting of the build.
The .aspx.cs will be compiled, along with the .aspx upon first invocation.
This allows to edit your deployed aspx.cs and have the result available immediately (which can be seen as handy, or risky)
As per my best knowledge, whenever we build the project, all the code
will build to dll in bin folder. Then why this file is required .cs
file?
This is the case when being in the default case, having a CodeBehind attribute in the page directive,instead of a CodeFile attribute
See CodeBehind and CodeFile attributes in documentation for #Page element
Note that you can use CodeFile attribute in Web Application Projects. I do it all the time.

ASP.NET App_Code in folder

I am working on a ASP.NET VB project. The host is pretty restrictive, so the App_Code folder has to be placed under the wwwroot folder.
My current folder structure (on the server) looks like this:
root
<-- Cant create folders or files here, host is restrictive.
wwwroot
App_Code
Class.VB
Styles
style.css
default.aspx
web.config
It seems to be working, if Class.vb contains errors, i get a compilation error on the website, so i know its compiling the class.
But i am unable to use the class in my other code files.
Example:
Dim emailFilter As Validation = New Validation()
I get this error in VS2010:
Type 'Validation' is not defined
How do i use the App_Code folder when its inside another folder?
Could it be an issue with namespaces? I might be more inclined to create a library for your validation class and add a reference to it instead.
I did find some similar issues to yours and they all referenced setting the Build Action for the class to 'compile'.
Classes residing in App_Code is not accessible

How asp.net application works?

I am quite new to .NET development and I am just wondering how does it work?
My undermentioned points are:
While developing ASP.NET application, under the project we have files like:
pagename.aspx
pagename.aspx.cs
pagename.asp.desiger.cs
After adding certain functionality to pagename.aspx page, assuming I have the development required web application (this is not my concern, what is developed)
Now I'm going to deploy this application, I use web deployment MSI which creates the required files in the one folder called folderdelopyed.
This folder contains the files required to support this application but interesting does not contain pagename.aspx.cs and pagename.aspx.designer.cs files.
My question is if folderdelopyed does not contain .cs file, then how does it work to run the segment of code which I have written in this file called PageName.aspx.cs?
The code in your cs files gets compiled into a dll.
For Web Application projects this is one dll
For Web Site projects, this is a dll per page.
All of the code is now in the dll's in the bin folder of the website.
You can use a tool like ILSpy (http://wiki.sharpdevelop.net/ILSpy.ashx) to look inside the dll's and see your code.
In the old days, for classic ASP, the script used to be embedded in your page - a mix of code and HTML, and was interpreted at runtime.
I like the new way more :-)
ASP.NET code is compiled into Dynamic-link library files, also known as DLL files.
The code you write in your code behind, which is the files with .cs extension, is compiled and put into whole new file, with .dll extension - and that file is copied to the server, to the BIN folder of your site.
Depending on what project type you choose, it's possible to have several DLL files for the web application, changing in every build - see dash's answer for more details.
On every .aspx page you have referece to what DLL file to use, as the very first line. For example:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="pagename.aspx.cs" Inherits="MyNameSpace.pagename" %>
In this example, the Inherits part determines what DLL to use. How? By the namespace, which is also the name of the DLL file.
When the above .aspx is requested by a browser, the .NET engine will go to the BIN folder, look for MyNameSpace.dll and in there look for class called pagename that inherits from the base Page class - all the rest is typical life cycle of ASP.NET page.
let me to say you something more Amazing.
you can hide your aspx file too.and put their content in to dll as same as your cs file put in dll.
you can make k aspx that just contain an address to the ddl file and no html body :D
that was greate!!! not only you can hide your cs file, you can hide you aspx file too :D

How to remove namespace from Inherits Attribute without a Parser Error, Could not load type

I've seen lots of posts about the Inherits Attribute, and the Parser Error "Could not load type"
I can get this working by putting "RootNamespace.PageName" for a specific page, where RootNamespace matches the Root namespace in my project properites.
But I would rather not put the namespace in there. i.e. I would rather put "PageName" than "Namespace.PageName".
I have a library project with a few DLLs and 10 or so .aspx and .ascx files.
To get an update of my library project, other projects in my company copy the DLLs in and then copy the .aspx and .ascx files into a specific folder in their project.
Only problem is every time they copy they have to change the Namespace of the inherits attribute to match the root namesapce in their project.
If they don't do this, they get no compiler errors but just get a Parser error when they hit the libary .aspx and .ascx files.
This is very annoying, it seems very ridiculous that so many pages will not work if the project root namespace changes.
Does anybody have any ideas on how I can make library pages and user controls for nuse withing other peoples projects?
Thanks,
Mike G
Ah ha! A colleague stumbled upon a way around this by accident...
OK I have a single shared "Library" project and many "normal" projects that make use of shared stuff from the Library...
1- Create a "Library" WebApplication that outputs a DLL, and put your web library code and also .ascxs and .aspx pages into the WebApplication project. 2- Reference the "Library" DLL in your "Normal" projects 3- Copy just the shared .aspx and .ascx files from "library" into the "normal" projects, but ... (important bit!) ... without the code behind
In our example we don't actually include the copied .ascx and .aspx files in the project (e.g. They're not referenced in the .vbproj file) and they don't get put in source control, they just get copied in from the library every time you build. We haven't experimented with what happens if you tell the project about the .aspx and .ascx files but they definitely load OK at run time.
So it does actually make sense no I think ab out it.
Basically the root namespace of the .aspx files is unachanged it's just refers to classes in the referenced library DLL so it all works.

Not able to create an instance of a class

In a Project I have different modules or folders.In a module I have a namespace called idsobject.I have class in this namespace.
In another CS fle in the same folder I'm trying to access this class.But i'm not able.while if put that cs file in aPP_code than i able to get that class.
how to access the class in another cs file within the same folder.
thanks in advance
In an ASP.NET web site project, *.cs files are only compiled and made available to the rest of the application if they're located in the App_Code folder, or a subfolder in App_Code, or if they are associated with a *.aspx as code behind. The architecture is intended to allow full server-side compilation. You can't just put a *.cs anywhere and expect IIS to be able to find it, compile it, and link it with the rest of the app.
With an ASP.NET web application, you can put *.cs files where ever you want, because they are compiled by Visual Studio using details that are kept in the project file.
I'm guessing that you're using a web site project, which is why it works when you put the file in App_Code.
First check the namespace. If you accessing the correct namespace then perhaps the class access is set to private.
You could give us some extra information.
This sounds strange. Putting a class in a file in a Web project with a .cs extension should work just fine no matter which folder the file is located.
The only thing I can suggest is to make sure that there is at least a
using idsobject;
line at the top of the code unit where you are trying to access the class. The default behaviour for ASP.NET Web forms is for no namespace to be defined, which can sometimes lead to confusion.
The class should be marked as public if you've tried to access it from another namespace.
If it doesn't help, please post here a problematic code snippet.

Resources