6

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?

sharptooth
  • 167,383
  • 100
  • 513
  • 979

2 Answers2

6

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.

devio
  • 36,858
  • 7
  • 80
  • 143
  • This is the preferred way to do it but I'd add that you should make a copy of your `.csproj` (for when you break it) and make sure it's not open in VS when you edit it with your favourite text editor (otherwise VS doesn't always reload it properly in my experience) – Basic Sep 19 '12 at 16:22
  • 2
    This won't precompile, this will only validate the pages. – sharptooth Sep 20 '12 at 07:01
1

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

Dai
  • 141,631
  • 28
  • 261
  • 374