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

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

Related

App_Code folder not recognized in "WebSite" project

I built a Web Site project (not Web Application) in Visual Studio 2017. I have a couple of generic classes in App_Code folder which are required in many Code Behind pages. It works perfectly in my local environment. But, when I upload them to the server, I get following compilation error.
CS0246: The type or namespace name 'UserInfo' could not be found (are you missing a using directive or an assembly reference?)
I created the App_Code folder by right clicking on the project; and even those classes were created there (not copied from anywhere outside).
Now, when I look at the solutions here, people are often mistaken Web Site with Web Application. In Website you cannot change the "build" to Compile; you don't even see the "build" option under class properties menu. OR my Visual Studio hiding that option from me?
Can anyone help me out please?
How are you uploading to server? Simply XCopy or Publish? Try publishing. It should solve the issue.

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

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.

Visual studio unable to detect Sitecore.Web namespace

Currently I'm playing with sitecore 7. I followed this blog to setup my visual studio with the sitecore.
Everything is fine until when I tried to create a sc:placeholder on the aspx.
It throws this error regardless whatever sitecore control I have tried to insert:
Error 180 The type or namespace name 'Web' does not exist in the namespace 'Sitecore' (are you missing an assembly reference?)
C:\inetpub\wwwroot\abc Sc7\Website\layouts\Customized\MainLayout.aspx.designer.cs
I tried to create a placeholder using code behind, no error was thrown.
I have tried to create an empty web application project. And added a reference to only Sitecore.Kernel.dll
I created a dummy page to test out the reference to the dll thing.
It still throws the same errors.
I know the blog mentions it, but have you added a reference to the "Sitecore.Kernel" DLL in your project? Open your references folder and ensure that there is not a problem with the reference.
Make sure your project is using .net 4.5 (you will need to use something other than VS2010)
After default sitecore installtion Sitecore.Kernel assembly is placed in bin folder. Maybe you used 'Clean project' and after this operation your assembly was deleted. Please check if this assembly is present in bin folder. It should be loaded by IIS automatically from bin folder, even if you have wrong reference in you csproj file.
When I start new project I always move dlls from bin folder to other folder, and put references to this files in csproj, so when I run clean project (or clean solution from VS) i have empty folder (only one file is present - *.lic file).

How do I specify an assembly file name for a compiled ASP.NET application?

I have read under: Compiling Web Application Projects (http://msdn.microsoft.com/en-us/library/aa983464.aspx)
... this compilation model creates a single assembly, you can specify attributes, such as assembly name and version.
You can supply a PATH (directory name only) in the Visual Studio "Build Options".
But where can the file name for the assembly be specified?
For a C# ASP.NET Web Application, see the AssemblyInfo.cs file, it's located in a special folder named "Properties". For VB.NET, you usually have to select your project and click the "Show All Files" button: Then you should be able to find AssemblyInfo.vb in the special folder "My Project".
Depending on the version of .NET and Visual Studio, the AssemblyInfo file may be located in different locations and it may not be visible per default, but it's always been named AssemblyInfo and it's always there, hiding somewhere. :)

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