简体   繁体   中英

Precompiling Web Application Project in VS WITHOUT Web Deployment Project?

I want to precompile my entire ASP.NET Web Application Project inside Visual Studio 2010, mainly to make sure there are no errors in the pages and controls.

How can I do this without using a Web Deployment Project? I tried using a WDP, but it has a massively annoying bug that causes it to compile all files in the application folder, including files that have been "excluded." It is maddening.

To be clear, I understand how to compile my application in VS. I also know how to deploy it. What I want to know here is how to PRECOMPILE the entire application (including all ASPX and ASCX files) BEFORE deployment. This is important to me because it is the only way to make sure there are no errors in the ASPX or ASCX files before runtime. I understand that there are command-line tools for this. I want to precompile inside VS, however, so that I can quickly correct any problems found.

So is there another way to do it? Is there some way to customize the Publish command?

Really appreciate the help, as MS documentation on this subject is frustrating, repetitive, and far-flung.

Brian

Years after the question, but some people might find it useful. I'm using VS 2013, but used this in VS 2010 previously too.

Go to Tool/External Tools, Add button. 
Title: Pre Compile 
Command: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\aspnet_compiler.exe 
Arguments: -p $(SolutionDir)\Web  -v / 
Initial directory: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ 
[TICK] : Use Output Window

Obviously, change the path to a suitable .net framework. You can click on the errors and the file opens within VS on the correct line. Weird things is that sometimes I run it multiple times and get different outputs...

The downside to this is that it doesn't tell you when it's done (you can always check for aspnet_compiler in the task explorer!). So I've done a DOS batch file for the only purpose of saying "done" in the Output window. Note that the command line is different to the above, as in this version I've replicated the command line effectively generated by our own msbuild script (it uses "fixednames" for example to resolve circular references precompiler errors). The %~f1 is the "argument" input box of the External Tools window from VS.

Batch file (precompile.cmd):

@echo C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_compiler.exe -p "%~f1" -v / -f -c -fixednames c:\temp\SomeFolder
@c:
@cd C:\Windows\Microsoft.NET\Framework\v4.0.30319\
@C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_compiler.exe -p "%~f1" -v / -f -c -fixednames c:\temp\SomeFolder
@echo.
@echo Done.

In VS:

Go to Tool/External Tools, Add button. 
Title: Pre Compile 2
Command: C:\mypath\precompile.cmd
Arguments: $(SolutionDir)\Web
Initial directory: [leave blank]
[TICK] : Use Output Window

PS: not sure how/if this is affected by Web Sites or Web Applications.

So, here's my current solution, which is far from ideal.

  1. I created a One-Click Publish profile to publish the application to a local folder on my machine. This local copy of the site can be used an an input to Aspnet_compiler, which does the precompilation.

  2. I created a new build configuration called "Precompile."

  3. In the project file, I created a new "Target" that only runs when the Precompile configuration is active (see below for syntax). This basically tells MSBuild to run an extra task after compilation; that is, it will run Aspnet_compiler to precompile the site. It takes the site files generated by One-Click, precompiles them, and outputs them to a second local folder. If any errors are found during precompilation, they are displayed in VS.

This is a totally half-baked solution with glitches. The biggest is that any errors found by Aspnet_compiler point to the files in the One-Click folder, so if you double-click on them, they take you to those files, not the solution files.

It's nice that I can control precompilation with this method. The task does not run unless I select the Precompile configuration. But the whole thing feels roundabout and silly.

Maybe a better solution will emerge.

Here is the element that I added to the project file, at the end, inside the main <Project> element:

<Target Name="AfterCompile"
     Condition=" '$(Configuration)|$(Platform)' == 'Precompile|AnyCPU' ">

    <AspNetCompiler PhysicalPath="[Source files created by One-click]"
    VirtualPath="/" Force="true"
    TargetPath="[Destination folder]" />
</Target>

Build the solution. Make sure that web application is not excluded from the build, but checking in solutions property pages.

Can't remember whether 'Exclude website while building solution' is for web application project / website project or both.

I have to post another answer as I could not attach the image in the comment.

请参阅构建网站作为解决方案选项的一部分

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM