Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 253
sharptooth Avatar asked Sep 19 '12 14:09

sharptooth


2 Answers

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.

like image 182
devio Avatar answered Oct 10 '22 21:10

devio


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

like image 28
Dai Avatar answered Oct 10 '22 22:10

Dai