How ASP.NET compiles its assemblies generally confuses me. It seems that I cannot program against anything outside the App_Code folder. In the application I am working on I have several server side controls and a few user controls. The user controls are outside the App_Code folder and I cannot refer to them from the App_Code folder. It is a "Web Site" project.
How can I refer to the UserControls from the App_Code directory?
Is this a compilation issue?
Thanks in advance.
Yes, it is a compilation issue, read a bit more about this same question at
http://www.velocityreviews.com/forums/t119801-accessing-web-user-control-from-class-in-appcode-folder.html
Whilst I'm sure suggesting you convert the web site to a web application is something you have considered doing and rejected, it may be the way forward if accessing your user controls is something you're looking to do frequently.
The way that I have gotten around the app_code issue is by putting all of my classes into a separate project and include the dll from that project as a reference in the web project. That also allows me to reuse the classes in other applications. I believe this is a common practice for business logic and data access when building an application using an n-tier environment.
Are you sure you're referencing the namespace which contains the user controls?
Related
I have a several projects full of .ascx UserControls that contain C# code blocks rather than having codebehind pages. I'm looking to consume a WCF service I've created, and I've added the Service Reference to the project, but I'm having trouble referencing it on the page using various directives.
Is this possible? The UserControls are used inside of WebParts in SharePoint 2010 if that makes a difference. Web service is deployed to _vti_bin if that matters as well, and is definitely operational (I've tested the exposed methods via a console app and through deployable projects which utilize codebehinds).
If this were not being done for SharePoint (yes, it makes a huge difference!) then I'd say that you should create a class library project, add the service references to the class library, and then use the class library from your user controls.
However, since it's SharePoint, you have to be concerned about deployment of DLL files.
I think some variant on my suggestion would work. You would have to sign the class library and place into the GAC, or you should arrange to bin-deploy it. In either case, I believe you'll want to include your controls and this class library in the same Feature.
I have a web application that contains a bunch of classes in the App_Code folder. I compile the project and publish it to the IIS server.
I want to share some of the code in the app_code folder with another application on the server and therefore I think I need to register an assembly in the GAC.
What is the best way to do this? I have read this article: http://msdn.microsoft.com/en-us/library/Aa479044, which suggests a number of options?
Put the code in a class library, and add the library as a project reference to both applications.
Side Note:
If you need to access the request or response, etc. import the Sysyem.Web library and use the HttpContext object. This will give you most, if not all the information available to the page.
You'll have to move the code into a separate project, which will output a library.
If you have any references to dlls related to the ASP .Net or web in general, you can reference them from that library.
The code might not compile in the first, but you can refactor it, it really depends on how tight is with what is in App_Code.
You can then reference that library on the Web Site (you'll have to refactor here too some things). The library, once is signed, can be added to GAC also.
The solution for me was to expose the shared functionality in a web service.
The message sent to browser is as follows:
The type My.API.Class is ambiguous: it could come from assembly '[on Temporary ASP.NET Files]' or from assembly '[on bin folder]'
The problem occurs when debugging a Web App, specifically when making a request to a WebMethod of a WebService.
The project compiles just right. It generates My.Website.dll on bin folder and if I publish the Web Application. It works fine.
The asmx file is on the root of the application. The CodeBehind file is on App_Code and its marked to be compiled to generate My.Website.dll.
I should be missing something really important.
I found someone having the same issue with a possible related cause. Check it out. The way this person exposes its problem is somehow similar but I get starting to be lost when he talks about a proxy class and shared dlls I don't use.
Any help is appreciated.
According to this, the App_Code folder should be used only on Web Site projects. That's the reason the CodeBehind of the asmx was compiled at runtime too.
The initial question was made based on a Web App. But I didn't specify this Web App. was been manually changed from a Web Site project.
To solve my problem I did the following:
Convert the project to Web Application. This will make App_Code to be renamed to Old_App_Code.
Moved all the Old_App_Code resources to a Class Library and then referenced this library into the Web Application.
#Tony: Thank you for guiding me.
The best way to debug issues like this is to use the "Modules" window in the Debug->windows menu of Visual Studio. It will show you all loaded assemblies. In particular, you want to look for My.API.Class in the modules list more than once. Sort the list by order loaded, and look at the dll right before it (that's usually the one responsible for the assembly to be loaded). The most likely cause of this is that one of your references also references My.API assembly, but references a different version of the assembly than your site does.
You can also fix it by adding your assembly name at the end of the attribute "Class" ex :
Instead of
<%# WebService Language="C#" CodeBehind="~/App_Code/WebService.cs" Class="WebService" %>
Use
<%# WebService Language="C#" Class="WebService, YourProjectName" %>
When you're making a web site, the assembly name is something random starting with "App_Code" but when you change it to a Web App the assembly name will be "YourProjectName".
This problem is only on development environment, so I guess my solution is better because you won't have to rename your folder (thie could cause problem with your source control).
Is it possible to share the App_Code directory and master pages with a nested application? I have a third-party newsletter application installed in my web app and I want it to share the same look and feel as the "parent" application. When I try, though, I get an error saying that the child application can't reference any of the exterior files.
I've done quite a bit of searching and found a lot of pages on overriding the Web.config in a child application, but I haven't been able to find anything that quite fits my situation.
The web server is running .NET 2.0 with IIS 5.0.
I have never tried this, but it might be possible to access the master page of the "outer" application by implementing a VirtualPathProvider in the nested application.
Can I somehow utilize the App_code folder in a web application project to compile code on the fly? It'd be great for plugins. Recently Rob Conery demonstrated its use in his talk at MIX 09 in a ASP.NET MVC app. I tried to do the same in a web app but I can't access the classes under App_Code from anywhere else. But if Rob was able to do it in an MVC app, it should be doable in a web application too. After all ASP.NET MVC IS a ASP.NET Web Application under the covers.
If you add a code file to the App_Code folder, it should be compiled and available from a code-behind file for a control, or another code file in the App_Code file.
I don't think you'll be able to access it directly from a compiled assembly, since the compiler won't be able to find that reference at compile time.
You'll also need to be aware that App_Code is compiled into a different assembly than your code-behind code, so you can't access internal code across the different assemblies.