I am relatively new to ASP.NET programming (but not programming in general), and I have been looking through a project that has been handed off to me. Within this project, there is a bin directory which contains a slew of various DLL files.
Then, in the web.conf file, inside the assemblies structure (within the XML), there is a slew of other assemblies being added.
I've done a search on both SO and through Google in general, and I am still struggling over what the difference is between the two. Is one way "better" than the other? Any clarification that can be provided would be most appreciated.
Thanks.
There are several ways to reference assemblies (DLLs, usually) in an ASP.NET application:
Add the DLL to your application's "bin" directory. This creates an implicit reference in every code file and ASPX file in your application to that DLL. This means that code inside an ASPX file or inside a CS file in App_Code can use the types in that DLL.
Add a reference to the DLL in the <assemblies> section in web.config. See MSDN for details on the syntax. There are generally multiple web.config files that apply to a particular web application. The application itself can have several web.config files (for example, a web.config in a particular folder might add its own references). There is also a global web.config (or machine.config) that has references available to all ASP.NET applications on the computer.
Use the <%# Assembly Name="" %> directive in a particular ASPX (or ASCX or MASTER) file. See MSDN for details on the syntax.
The references in a given file in an ASP.NET application is a combination of the applicable items above.
The reason you have to reference assemblies is that ASPX files (much like CS and VB files themselves) eventually get compiled by either the C# or VB compiler. In order for those compilers to know what types you want to use they need to know which assemblies contain those types.
The bin directory is a copy of all the dll's that are built and referenced by your project. When the web application is running, it looks in the bin directory for the physical dll's it needs to execute the web application.
Related
I have one quesion, let's put I have a project with 3 web forms and each form has inside 5 connection strings or any other static variable so total would be 15.
And let's assume that I can't deploy the dll because I am told to use aspx.cs and aspx files only.
If I don't want neither store the connection string inside the web.config I can create a normal class (no aspx.cs only class.cs) where I create a static Constant for using it in my classes.
In the end I have 3 aspx.cs files, 3 aspx and one cs, but if in the IDE it works when I deploy it says "couldn't find reference to Class."
Why is that? It would be very useful for storing variables.
There are two ASP.NET project types:
Web Application Projects: Here, the code in aspx.cs and .cs files is compiled into a DLL. If you cannot deploy the DLL, you cannot use this project type.
Web Site Projects: That seems to be what you are looking for. The code is compiled at run time; you just need to make sure that your shared code (your .cs file) is located in the App_Code folder.
If you project is currently a web application project, you need to change it to a web site project or change the opinion of the person who told you not to use DLLs.
When you deploy using the Web Site project model, the class files (.cs) need to be placed in a special folder called App_Code.
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).
I'm looking at an asp.net application, i notice that there are assemblies defined into two places. In web.config there is configuration/system.web/compilation/assemblies/add elements. In the project file there are references setup under the Project/ItemGroup/Reference elements.
I was wondering, what is the difference between assemblies/references added in either location?
In the web.config section:
The assemblies element defines a collection of assembly names that are used during compilation of an ASP.NET application.
Web site projects usually use the assemblies element as there is no project file storing location of references that the web site uses. The project references would not apply to a web site, as it has no proper project file to store these in, so must store all referenced assemblies in the web.config. There is some interesting, although not directly related, discussion here.
Web application projects may make use of both the assemblies element and project references.
Having a reference in the assemblies element also means you won't have to add the #register at the top of any .aspx pages that use that namespace. More discussion on that here.
We have a website which is already deployed in the production server. We need to remodify the application and then re-deploy it. But we do not have access to the code-behind files as everything is compiled into a dll.
Your options are basically
1) Modify aspx / ascx files to alter text content / styling, though you won't be able to add any functionality here (well not server-side functionality anyway).
2) Reverse-engineer the DLL to produce the code-behind classes which will only produce readable code providing the assembly wasn't obfuscated. You could look at Redgate Reflector for doing this. (It's a useful developer too to have anyway).
As far as getting the files into a solution is concerned you could try creating a new web application project (deleting the Default.aspx and it's web.config which are generated as standard) then adding the aspx/ascx files into this project.
A couple of questions:
Is App_WebReferences for WCF schema files? Should this not be App_ServiceReferences?
Also, what is dynamic compilation in ASP.NET?
Thanks
From MSDN:
App_WebReferences folder Contains
files used to create a reference to a
Web service (in the same project or
external to the project), including
.disco and .wsdl files
There's no special folder in ASP.NET called App_ServiceReferences.
As far as dynamic compilation is concerned you may take a look at this article which explains it very good.