Adding references to external dll in asp.net (VB) - asp.net

I have got a .aspx file containing code to look up data from a MySQL server. When I use visual studio (VB) and import the .dll my code works fine. However, I am not sure how to reference the .dll in asp.net (I am using notepad for creating my .aspx file, and hosting using Microsoft IIS 8.0)?

.NET Applications look for DLLs referenced in their code in the 'bin' folder of their project, and the GAC (Global Assembly Cache) where you put system shared DLL's, if not found in the local 'bin'.
If you ensure that you have your DLL in one of the places, you can develop code as you would do in VS after referencing the DLL.
Another option is, adding the <%# Assembly Name="" %> directive in that a particular ASPX, or to the <assemblies> section in web.config if you will need to use it in more than one of your ASPX pages.

Related

How do I make Visual Studio "compile" a .aspx file?

In my ASP.NET MVC application I have a .aspx file. I select it in Visual Studio 2010 Project Explorer tree and go to file properties - the "Build Action" is set to "Content". I change "Build Action" to "Compile" and ask Visual Studio to build the project. I get the following error message for my .aspx file in the compiler output:
C:\PathToProject\MyFile.aspx(1,1): error CS0234: The type or namespace name 'global_asax'
does not exist in the namespace 'ASP' (are you missing an assembly reference?)
the first line of the .aspx file is:
<%# Page Language="C#" MasterPageFile="~/Views/Shared/My.Master" Inherits="System.Web.Mvc.ViewPage" %>
which makes me wonder...
What is the compiler trying to complain about and how do I make it compile the .aspx file?
The view's .aspx must have its Build Action set to Content.
To enable pre-compilation (or, at least, compiler error checks) at build time, add
<MvcBuildViews>true</MvcBuildViews>
to the first <PropertyGroup> in your .csproj.
As #KennyZ says, ASPX/ASCX/Master/CSHtml files are not "compiled" - not as part of the regular build process anyway. That's because these files are compiled into Page classes on first-request, this is to allow webmasters to modify the files on-the-go, which is generally speaking a good idea, especially if the ASPX files contain a lot of content.
Note that the VS File Properties Build Action does not control this setting - I think the BuildAction property should be hidden or at least better documented - it isn't very well understood by the developer community.
But it can be done! In VS2005 when they introduced the ill-fated "web site" projects (as a replacement for VS2003 "Web Applications" until VS2005 SP1 came out) there was an option to pre-compile the ASPX/ASCX/Master files into the application's assembly - it did leave behind stub *.aspx files that didn't contain any content, but instead referenced the pre-compiled page classes.
You can still do this with VS2010, but you need to use the command-line aspnet_compiler.exe as the GUI for it doesn't exist for Web Application projects.
There is more documentation available here: http://msdn.microsoft.com/en-us/library/bb398860%28v=vs.100%29.aspx
and here: http://msdn.microsoft.com/en-us/library/ms229863%28v=vs.100%29.aspx

How to reference external assemblies from an ASP.NET web site

I need to reference external assemblies from a dozen asp.net web sites. These assemblies won't be in the GAC, but will be located in a location relative to the web site ("..\..\dlls\release").
From what I understand, Visual studio doesn't have an explicit assembly reference, or way to link a web site to an external .csproj or .dll library. (Unlike a web project).
These sites are sample projects for the open-source image-resizing HttpModule http://imageresizingin.net/.
I don't want to make users run a .bat file to copy the dlls into each /bin folder.
Is there some secret hints file I can use to let visual studio know where to look?
I'm working on dynamic assembly resolution, but that won't help visual studio at all, just hosted versions of the site.
Add the assemblies' full names to the <compilation> element of Web.config, and put them in the GAC on your dev machine.

In Asp.Net 2.0 web site project and Assembly Reference?

Where is the information about the Referenced Assembly kept in an Asp.Net 2.0 Web Site project. Since, there is no .csproj file here we don't know where to look for this. We are facing problems because of multiple version of log4Net being referenced and want to remove one of them.
The only information a website has is the fact that the DLL exists in the bin directory of the website. When you add an external DLL to a project, that DLL is copied into the bin, and a .refresh file is added that provides Visual Studio with the path to the original DLL, so that it can refresh the DLL when it changes.
As far as I know, the assembly tag in web.config is only for GAC assemblies that are added to a project, not external DLLs.
As to your specific problem, if you have class libraries, it is possible that one of those references a different version of the DLL. It is also possible that you have multiple websites and one of them has a .refresh file that is pointing to a different version of the DLL.
In the web.config file under "assembly" tag

How do I compile an ASP.NET website into a single DLL file?

I have finally finished my web site. I published it, and I was surprised at the results. Although the App_Code compiled into a single DLL file, every page's code behind compiled into its own DLL file. How do I make it so that it is one DLL file?
Also, is it possible to compile everything (SubSonic, AJAX, etc.) into that same single DLL file?
You might prefer to use the web application project style for that.
You can use ILMerge to merge assemblies into one.
The way we do it is by adding a deployment project to our site:
http://msdn.microsoft.com/en-us/asp.net/aa336619.aspx
To accomplish this you will have to covert your project into a Web Application Project (Supported in Visual Studio 2005 SP1 and Visual Studio 2008).
The process of converting is not that hard, but you will need to move everything out of the app_code folder, as WAP (Web Application Projects) projects do not have code inside app_code.
Once you do this, everything inside your project is compiled into a single DLL file, any external assemblies are still contained in their own DLL files though, but there are options around that as well.
We use build scripts for our websites and run the aspnet_merge.exe from the command line. Here's the MSDN page: http://msdn.microsoft.com/en-us/library/bb397866.aspx

CodeFile vs CodeBehind

What is the difference between CodeFile="file.ascx.cs" and CodeBehind="file.ascx.cs" in the declaration of a ASP.NET user control?
Is one newer or recommended? Or do they have specific usage?
CodeBehind: Needs to be compiled (ASP.NET 1.1 model). The compiled binary is placed in the bin folder of the website. You need to do a compile in Visual Studio before you deploy. It's a good model when you don't want the source code to be viewable as plain text. For example when delivering to a customer to whom you don't have an 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 Microsoft.NET[.NET version]\Temporary ASP.NET Files.
I'm working with an Application Project in Visual Studio Express 2012 For Web and using .NET 4.0. In the code behind files for my login and change password pages I found a situation where I needed both CodeBehind and CodeFile in the declaration.
If I don't add a code file reference like
CodeFile=login.aspx.cs
The web page doesn't parse and the browser displays a parser error. It doesn't matter whether I compile the project or not.
If I don't add a code behind reference like
CodeBehind=login.aspx.cs
References to Security classes like MembershipUser fail both at compile time and when attempting to use intellisense with an error like "The type or namespace MembershipUser cannot be found". I have added a reference to System.Web.ApplicationServices as required by the .Net 4.0 framework.
I should add that these troublesome files are running in an application within the website created using the IIS Application tool. When I open the website from Visual Studio I have no difficulty with parser errors or reference errors. This confusion only occurs when I open the application as a project in Visual Studio.
Codebehind file need to compile before run but in src we dont need to compile and then run.. just save the file.

Resources