In ASP.NET web pages we use special tags like <%# %>,<%= %>,<%# %>and<%$ %>.
I'm just curious if those tags are compiled or not? Do we have to build the project to make it work, or is it compiled at run time?
Usually that code is compiled at runtime when the page is first hit. Depending on your settings in your web.config file it compiles in Debug or Release mode.
It is possible though to pre-compile that code before you deploy your project. See How to: Precompile ASP.NET Web Sites for Deployment. It uses the aspnet_compiler to compile the ASP.NET code to an assembly.
They are recompiled first time page code is executed after web application starts or the page is changed.
It is compiled when it is loaded into IIS, and if you have selected the precompile option when you build the website, they have been precompiled, just like with a DLL.
Related
I'm using the VS2019 IDE. I have a solution with various class library projects and a website. The website is not in a csproj file; it's a locally hosted IIS website and VS2019 lists it as http://localhost/websitename/ in Solution Explorer. Everything is targeting .NET Framework 4.7.2. The website has the Microsoft.CodeDom.Providers.DotNetCompilerPlatform.3.6.0 NuGet package installed.
When using a newer language feature like the abbreviated new operator in this example:
StringBuilder sb = new();
...the class libraries compile just fine. Within the website, the background compiler doesn't complain about this, either. But if I build the website, I get:
CS8124: Tuple must contain at least two elements.
CS1526: A new expression requires (), [], or {} after type
If I change the code back to the classic way:
StringBuilder sb = new StringBuilder();
...the website builds fine, but the background compiler gives the following "message":
IDE0090 'new' expression can be simplified
The csproj files for the class libraries all contain:
<LangVersion>latest</LangVersion>
...and the web.config contains:
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=3.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:latest /nowarn:1659;1699;1701" />
What am I missing?
PS: I don't necessarily need the latest language features. I'd be fine if the code just compiled the same way between the background compiler and the one that's used when VS does builds.
Ok, it looks like you have Rosyln enabled for development, but those extensions are not setup and deployed on the web server.
There are several solutions - which is best for you will depend on if this is a asp.net web site, or a asp.net web site applcation.
So, you could remove + dump roslyn from your project.
Another way, is if you using an asp.net site applcation, then the site is pre-compiled by VS and NOT by the web server. I perfer this option, since then you get FAR BETTER resoltuion when refercing external assemblies and classes.
However, the HUGE down side? To change ONE line of code behind, then you have to do a full re-publish of the site.
However, as noted, I find the benefits FAR outwiegh the down sides.
So, one choice you can make is to use a asp.net web applcation, and deploy as a application.
However, while all code is pre-compiled BEFORE publish to the web server? the one area hat is NOT pre-compiled - and WILL be attempted to be compiled by IIS (the web server) is the App_code folder.
So, what I do is create a new "genreal" folder that holds your routnes and call it say MyCode. Then for the code modules (vb.net) or static classes (c#), you simple right click on those files->properties, and ensure that "build action" is compile.
eg like this:
So in above, you can see in place of App_Code, I created a new folder called MyApp_Code.
As a result, then IIS does not attempt and will not compile that code when I publish.
(in fact, the code and folder will not even exist after a publish).
As I stated, the above suggestion only works if you using a asp.net web site application and a "sln" project file. If you using an asp.net web site? Then you have to either get IIS setup to have the rosyln compiler extensions setup on the server, or simple dump (remove) the rosynln compiler extensions from your project.
By default (if my memory is correct), you see/find the roslyn extensions installed in your project via nuget.
How to remove the compiler extensions is outlined here:
https://www.c-sharpcorner.com/Blogs/remove-roslyn-support-from-asp-net-project
So, as noted, one of the above two choices will work. Due to building and using a lot of external library code, being able to global reference many classes etc., and having the site pre-compile before deploy, then using + adopting a sln and asp.net web site applcation is a FAR BETTER deployment model and coding model then using "just" a asp.net web site. However, some STILL prefer the asp.net web site approach, since you can modify one page, and even just one line of code (code behind), and deploy that one page, and you are done.
Some even open the production site directly. And a simple change to a aspx page can be done by hitting ctrl-s. With the application deploy model, you lose this easy deploy if you go with a "applcation", but for most, the benefits still outweigh the difficulty in changing code and THEN having to do a full re-publish of the site.
I don't have a handy reference as to how to install Roslyn support on IIS, and if you using a hosting provider, then you probably don't have that choice + option anyway.
So, in that case, you either remove Roslyn support from your project, or you adopt the web site applcation publish model. (and move any code in App_Code to another folder to force pre-compile of such code before you deploy.
Have tried adding this to web.config
<compilation debug="false" targetFramework="4.0">
</compilation>
but website still executes code in #if DEBUG when it shouldn't
*Wierdly the inline statement <% #if DEBUG %> on aspx files works but require also for .cs code.
NB development and live website on same box
#if directives in backend .cs files are handled by the C# compiler, not ASP.Net.
Set your project to the Release configuration so that that symbol is not defined when compiling the DLL in VS.
compilation debug element is not the same thing as DEBUG preprocessor directive.
You need to re-compile your website in Release mode.
What about binaries compiled with debug symbols?
One scenario that several people find very useful is to
compile/pre-compile an application or associated class libraries with
debug symbols so that more detailed stack trace and line error
messages can be retrieved from it when errors occur.
The good news is that you can do this without having the have the
switch enabled in production.
Specifically, you can use either a web deployment project or a web
application project to pre-compile the code for your site with debug
symbols, and then change the switch to
false right before you deploy the application on the server.
The debug symbols and metadata in the compiled assemblies will
increase the memory footprint of the application, but this can
sometimes be an ok trade-off for more detailed error messages.
http://weblogs.asp.net/scottgu/archive/2006/04/11/Don_1920_t-run-production-ASP.NET-Applications-with-debug_3D001D20_true_1D20_-enabled.aspx
Ensure the compile configuration to release as well. In Visual Studio, Build Menu > Configuration Manager and make sure "Release" is selected for all your assemblies, and/or is the active solution configuration.
I'm using the aspnet_compiler compiler program as part of the automated build procedure for a website. We have a website project and the build step generates the assembily for the project for the release build. However it generates these dlls in such a way that the aspx pages reference those at the moment randomly named assembilies. So my question is how would I modify this process to allow me to swap the dll's without updating the entire site. Also we're using nant at the moment for the build process.
Have you tried using the -u option? The reference is at http://msdn.microsoft.com/en-us/library/ms229863(VS.80).aspx.
The aspnet merge tool allows all the dlls to be merged into one explictly named assembily and updates the aspx and ascx files to reflect the appropriate assembily.
I am attempting to make it so that we can have our application to behave differently based on the presence of a preprocessor directive. I've read that you can create build configurations to define different preprocessor directives based on which build you do. Well, I am not seeing anything in Visual Studio to do this.. I know how to do it from the command line, but not how to do it within the automated environment of VS 2008.
Can someone tell me how to create a new build configuration which has preprocessor directives set in it?
Also, not sure if it has anything to do with it, but our project is an ASP.Net website
I do not even see a way to control the
DEBUG symbol, and it is an ASP.Net
website
Ok, there's the problem. The ASP.net web site option gives you a lot less control over builds, etc as it doesn't even use a project file.
If it is at all feasible for you to switch, switch over to an ASP.net web application. A web app will behave much more like a typical C# project (you have a project file, and the contents of the csproj file control what is in the app instead of just the directory structure, etc.
After you have converted, you should see the options that you are expecting.
Here's a link with directions for converting: How To Convert ASP.NET Website to ASP.NET Web Application
If you right-click the project and select "Properties", then select the Build tab, you can enter custom compilation symbols in "Conditional compilation symbols".
For example, if your code looks like this:
#if DEBUG
// do something
#else
// do something else
#end
You can set "DEBUG" as a Conditional compilation symbol.
You can set different values for different build configurations, by changing the "Configuration" drop-down.
In VS2008 (and earlier if I'm not mistaking) I have dropdown that lets me choose between Debug and Release mode. All well.
When I switch VS to Release, the debug attribute in my web.config files isn't set to false at all though.
Is this the way it is supposed to be? I am bound to forget to set it to the correct value on deploying.. What measures should I take to make sure this is working like it should?
This is one solution to this problem:
http://blog.aggregatedintelligence.com/2009/04/aspnet-never-again-fear-publishing-your.html
Well your web.config would probably be different for debug and release (connection string, passwords, etc...) but if it's not, look at a postbuild event which would copy over a different file.
Also check this blog post from Scott Guthrie.
Changing release mode will not change web.config, however when you build your web app, it will build the dll for only C# files in release mode where else your web.config's debug on/off is used by IIS to build debug/release version of ASPX markup files.
The build flavour just affects how the code is compiled, it does not affect your configuration files. So yes, to answer your question, this is how it is supposd to be.
The element is a good solution if you have access to the machine.config of your server, which hosts only production applications.
I usually modify the web.config file when generating the deployed files as part of the automated build process. For example web deployment projects can perform web.config section replacement. There are a number of reasons I don't like web deployment projects and I tend to do it with a simple VBS file that modifies the file using MSXML.
The answer you selected from Bobby is not correct. Visual Studio builds the files for you in release while you are in VStudio.
IIS compiles the code at startup with that setting when you deploy. Not the bin directory, but the App_Code and the code behind files.
You should precompile your app before deployment which will compile your code behinds and App_Code dir into dlls in the bin directory.
The deployment tools automatically switch that setting if you set the deployment tool to Release
I use web deployment projects. http://weblogs.asp.net/scottgu/archive/2008/01/28/vs-2008-web-deployment-project-support-released.aspx