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

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.

Related

ASP.net Web Site referencing code files with dependencies on non-compiled code

I have a website that is not utilizing the App_Code folder for jit compiling code files (that is what App_code is for, right?). Instead developers have to either cram all the necessary classes that they need for a page into the code file, or into an individual .cs file that gets and Assembly Directive src on the page (this is because of dependencies between the source files).
Is there any way for classes with dependencies on each other to be placed in separate files?
For instance Presenter.cs has a dependency on Model.cs. I would like to just add an assembly directive to my page for Presenter.cs, and make it happy. But since this does not compile Model.cs, it doesn't work. Adding Assembly references for both code files also does not work.
Is there a mechanism that I am missing?
EDIT: I think the answer is that the App_Code folder is exactly created for this task and that there is no other/better way to do it.
When you say "CodeBehind" are you actually referring to "CodeFile"? (One of the many reasons I'm not a fan of "ASP.NET Websites" and prefer MVC).
AFAIK, the ASP.NET runtime compiles all of the App_Code files together into their own assembly, however code in each page's CodeFile (not their CodeBehind) can only reference this anonymous assembly and not each others' code.
Can you clarify what you mean by "page assembly reference"? Are you doing <%# Assembly %> or do you mean a namespace import <%# Import %> (because as I said, a lot of the code is compiled into an anonymous assembly).
I suggest you just move the common code and classes to *.cs files within App_Code. It should just work.

consuming classes from folder other than App_Code

I am creating a site wherein I have a folder which contains some .cs files. I want to access those classes in .aspx and .ascx files. I’ve created some properties in it, but when I create the object of the class I don’t find that property via IntelliSense.
How can I use and consume those properties from that .cs file?
The website will only compile code files that are in the App_Code folder or are codebehind files for referenced controls. There isn't a way to reference classes defined in code files outside of the App_Code folder.
If you compile those classes and put the resulting dll in your website's bin folder, then you can reference them. To do that, you'll need to add them to a Web Application project in Visual Studio. See Ian Robinson's WAP blog post for most details.
Just have those properties public and you'll see them.
If still no luck please post your code and also tell: can you create instance of the class without error? Can you access any other properties or methods?

Page class outside of App_Code will not compile

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.

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.

asp.net web sites and default namespaces and LINQ Datacontext part 2

Let me try to ask this question from a different angle.
I noticed that everytime an aspx page gets rendered in the browser using the "web site" model, a random assembly gets created 'on-the-fly' in the Temporary ASP.NET files. Analyzing the assembly in Reflector shows that the class created for any given .aspx file is under the "ASP" namespace.
So, starting with a empty "Temporary ASP.NET Files" directory, I opened my ASP.NET "website" in VS2008, and launched the default page. Immediately I observed that a random directory was generated inside that folder. Working my way down the path, I found 2 DLLs created: App_Code.1lywsqqz.dll, and App_Web_iohekame.dll. I assume that all the .aspx pages in the website get compiled into App_Web dll and everything in App_Code folder gets compiled into App_Code.dll.
So if my App_Code C#/VB.net files are under the "ASP" namespace, and my App_Web files are created under the "ASP" namespace, how come I still get an error "Could not load type 'ASP.NothwindDataContext'?
Somebody said "you don't need namespaces in the App_Code folder", but I tried it without and still get "Could not load type 'NorthwindDataContext'".
So what's going on between the App_Code folder, the rest of the site, and namespaces?
EDIT:
Here's my LinqDataSource in my .aspx file:
<asp:LinqDataSource ID="LinqDataSource1" runat="server"
ContextTypeName="NothwindDataContext" EnableUpdate="True"
TableName="Categories">
</asp:LinqDataSource>
Neither "NorthwindDataContext", nor "ASP.NorthwindDataContext" works.
Types in App_Code C# source files, just like any C# file, will not be put in a specific namespace unless specifically declared by namespace Name {...} around it. So a class MyClass declared in App_Code will have the fully qualified type name MyClass. Just that.
You can reference it in Web.config as: "MyClass, App_Code".
Side note: When you are using a DBML in App_Code, the namespace of generated classes are defined in that file (look at the properties window when DBML file is open). If you specify a namespace in that file, naturally, your classes will be defined in that namespace. Note that this does not contradict with what I said above. The thing is, the LINQ data context generator processes the file and defines the classes in the specific namespace.

Resources