Precompile asp.net views with ms build - c#

When I deploy asp.net application through visual studio I know I can just check Precompile during publish and uncheck Allow precompiled site to be updateable.
And I want to do the same with msbuild tool and I am using /p:MvcBuildViews=true /p:EnableUpdateable=false but when I go to IIS and open views they still have their content which means they are not precompiled, right?
They should have line This is a marker file generated by the precompilation tool as they do when publishing from VS. Am I missing something?

Precompile asp.net views with ms build
You should use the arguments /p:PrecompileBeforePublish=true instead of /p:MvcBuildViews=true.
MvcBuildViews is often mistakenly considered as something that when activated results in precompiled views. Actually. It is just a thing to include views to build process but it doesn’t compile these to project binaries folder.
When we check the checkbox Precompile during publish and uncheck the checkbox Allow precompiled site to be updateable on the file publish options, we will get following properties setting in our FolderProfile.pubxml file:
<PropertyGroup>
<PrecompileBeforePublish>True</PrecompileBeforePublish>
<EnableUpdateable>False</EnableUpdateable>
</PropertyGroup>
So if you want to do the same with msbuild tool, we should use the arguments:
/p:PrecompileBeforePublish=true;EnableUpdateable=false
Besides, since those arguments are stored in the .pubxml file(under the PublishProfiles in the Properties node in Solution Explorer). They are now designed to be checked in and shared with team members. These files are now MSBuild files and you can customize them if you wish. In order to publish from the command line just pass DeployOnBuild=true and set PublishProfile to the name of the profile:
msbuild.exe "TestPrecompiled.csproj" /p:DeployOnBuild=true /p:PublishProfile=FolderProfile.pubxml
Of course, you can use the arguments and the .pubxml file at the same time, the arguments in the command line will overwrite the property in the .pubxml file:
msbuild.exe "TestPrecompiled.csproj" /p:DeployOnBuild=true /p:PublishProfile=FolderProfile.pubxml /p:PrecompileBeforePublish=true;EnableUpdateable=false
After publish completed, open the .cshtml file in the publish folder, we will get the line This is a marker file generated by the precompilation tool, and should not be deleted! as they do when publishing from VS:
See Precompiling ASP.NET WebForms and MVC Views with MSBuild for some more details.

Related

Add folders to a c# projects using dotnet cli commands

I know this can be done via Visual Studio. But I wanted to know if there is a command or a workaround to add folders to a project.
My projects are: Class Libraries a project for creating a class library that targets .NET or .NET Standard
I tried with a shell script:
mkdir DTOs;
mkdir Behavior;
mkdir Features;
mkdir Filters;
mkdir Interfaces;
mkdir Mappings;
mkdir Wrappers;
folders are created but they are not added to project.
Thanks in advance.
Please check out the following detailed answer by poke.
Paraphrasing it for reference here:
Project folders
In modern .NET Core projects (using the .NET SDK), files are automatically added based on a global file pattern. For example, any .cs file anywhere within your project directory is by default automatically configured to be a part of your project that needs to be compiled. This pattern however only applies to files, not directories.
Directories are not an explicit part of a project by default. Instead, they are only there if they are “needed” for a path to a file. That’s why you won’t see folders within Visual Studio until there is a file that is part of the project.
If you are within your project folder and then add a folder there, you will not see the folder there. But as soon as you add a file to that folder (echo '' > TempFolder\Test.cs), it should automatically be picked up by Visual Studio:
You can also enable the “Show all files“ option in the solution explorer, to make folders that are not part of the project appear in the solution folder:
As you can see, the folder appears as a transparent item because it is not part of the project itself. You can then right click on the item and choose “Include In Project“ to make this folder an explicit part of the project. This action will add the following section to the project file:
<ItemGroup>
<Folder Include="TempFolder\" />
</ItemGroup>
This basically tells Visual Studio that the folder is part of the project even though it does not contain any files. As soon as you do add any file to the folder, Visual Studio will remove that configuration though since the folder is now again an implicit part of the project.
Solution folders
Visual Studio solutions don’t show the actual directories within your solution directory but rather a virtual directory as configured within the .sln file. Projects being located within subdirectories will not automatically be located within such a folder within the solution structure, and similarly non-project folders will also need to be added to the solution file first.
There is no mechanism to manage the solution folders with the dotnet sln command though. The only thing that you can do is add a project into a particular virtual folder within the solution:
dotnet sln add path/to/project.csproj --solution-folder VirtualFolder
This would add the project.csproj inside the VirtualFolder solution folder within the Visual Studio solution.
If you want to manage the solution folders otherwise, you should do that with Visual Studio.

Azure build pipeline not generating bin folder

I have aspnet MVC 5 solution which is containing several project, i need to publish the main project using azure CI and CD so i configured azure build pipeline (see the attachment for configuration). but when the artifacts are generated i noticed that there is no bin folder with deployment files. what could be the possible reasons for this behavior?
This is the generated artifact
This is Solution Configuration task
This is the publish path
This is publish artifact configuration task
This is link for the logs of the pipeline
This is The error when i chose the solution instead of csproj
This is after chaining to SLN or set AnyCPU
what could be the possible reasons for this behavior?
According to the images you provided, I could reproduce this issue if I select the project file .csproj instead of solution file .sln in the Visual Studio build task:
That because if we select the project file .csproj, there is a warning when we build the project:
Warning : The OutputPath property is not set for project 'ASP.NETMVC.csproj'. Please check to make sure that you have specified a valid combination of Configuration and Platform for this project. Configuration='release' Platform='any cpu'. You may be seeing this message because you are trying to build a project without a solution file, and have specified a non-default Configuration or Platform that doesn't exist for this project.
Although the result of the task is passed, the output is not output correctly due to the above warning.
That the reason why you build is successfully, but bin folder is not generated.
To resolve this issue, we select the solution file in the Visual Studio Build task:
Then the warning disappeared, and bin folder is generated:
Update:
when i select the solution instead of the csproj i got errors related
to the unsuccessful builds due to some missing references.
That because you just restore the one of packages.config file in the nuget restore task. You need to change it to the .sln file instead of packages.config file.
Besides, to resolve your previous issue, we could choose the .sln file instead of the project file (I answered before.). Or you could change the default variable BuildPlatform from any cpu to AnyCPU (move the space).
Hope this helps.

Deploy asp.net mvc website with command on post-build event

i want to deploy mvc web site that build with visual studio,to the specific folder location.
how can it done with project post-build event command.
when project build should publish website to the specific folder location.
Two steps:
Create a publishing profile for your project using deploy to file system as a publishing method
On your postbuild event call the msbuild.exe with the DeployOnBuild=true parameter specifying you publishing profile.
I am assuming project is building successfully & has resolved all external references.
1. First set Target Location for a Directory like below image in web project:
this is optional but better than change debug/release from build path again & again.
2. Add something like below in Post-build events:
copy "$(ProjectDir)\bin\*.*" "c:\check\"
Hope it helps.
Update
Is this asp.Net mvc Web project or website? Web project can set output path to bin as in step 1. If you don't want that command in step 2 need path of compiled code as first parameter & path where you need to copy as second parameter, wherever they are. It uses copy command which is as old as DOS & Linux. Refer
https://technet.microsoft.com/en-in/library/bb490886.aspx for copy command.

How do I view the metaproj files generated by Visual Studio when building an ASP.NET website?

I have a fairly large solution with a mix of assemblies and ASP.NET websites (the ones without a csproj file). I'm trying to customize the build from the command line, and so I need to understand what exactly MSBuild is doing.
When MSBuild builds .sln file, I see that it creates metaproj files (which I assume are MSBuild files). However, they never seem to appear on the file system. I assume that MSBuild keeps them hidden in some way. Is there a way that I can view these files?
use the following environment variable:
set MSBuildEmitSolution=1
Some "under the hood" basics for Web Site projects that gets lost the way MS organizes their docs...
When MSBuild is executed on a .sln file it generates a .metaproj file based off of the "Project" section in the .sln file. It then executes aspnet_compiler.exe against the metaproj file.

How can I tell VS2010 to include files in my manifest for publishing?

I have an application that is published through the ClickOne mecanism. The issue I'm having is that it is not publishing my NLog.config file, which is required for my application to run. I've looked through the Application Files screen, but I don't see NLog.config as an option to select. My NLog.config file has a built action of Content and is set to copy to the output directory. If it matters the NLog.config file is in another project that is referenced in the project I'm publishing.
I'm aware that I can use MAGE to essentially scan my publishdirectory and have it update my manifest, but what I'm looking for is a way to do it automatically.
What are my options?
Possible Solution
One solution could be to configure NLog through code rather than XML.
I use this great tool too and I have a click-once program too:
In the Solution explorer, click on your nlog.config. In the properties window, put the Build Action as content.
Be sure that you see "Applications files" of the project's properties window.
Voilà!

Categories

Resources