Recently I was working in a value addition where there was a requirement of publishing WebApplication. I initially tried with searching of some form of aspnet_compiler API and found something like below.
————————————————————————————
string msPath = @”C:\Windows\Microsoft.NET\Framework\v4.0.30319″;
string msCompiler = “aspnet_compiler.exe”;
string fullCompilerPath = Path.Combine(msPath, msCompiler);
ProcessStartInfo process = new ProcessStartInfo
{
CreateNoWindow = false,
UseShellExecute = false,
RedirectStandardOutput = true,
WorkingDirectory = msPath,
FileName = fullCompilerPath,
Arguments = string.Format(“-p {0} -v /{1} -f {2}”, “\”” + solutionFileDirectory + strProjectPath + “\””, “/Web”, “\”” + txtBuildPath.Text + “\””)
};
Process p = Process.Start(process);
string output = p.StandardOutput.ReadToEnd();
——————————————————————————–
But there is requirement of adding modification of the output after aspnet_compiler is done with such as adding global.asax, deleting unwanted folders etc.So you need to add custom code to make those modification.
It is really a mundane task and also the output is also not perfect.
Recently I have found out a way to publish web application by writing some custom code of my own and then creating an exe out of a console application. You can create this by simply creating a console application and copy paste the entire code in the main method. BTW below namespaces are used to create this app. Also you need to add the below references.
Microsoft.Build.Engine
Microsoft.Build.Framework
using Microsoft.Build.BuildEngine;
using System.IO;
————-CODE TO BE PUT INSIDE MAIN METHOD—————
try
{
string solutionFilePath = args[0];
string strBuildPath = args[1];
if (Directory.Exists(strBuildPath))
{
string[] filePaths = Directory.GetFiles(strBuildPath);
string[] folderPaths = Directory.GetDirectories(strBuildPath);
foreach (string filePath in filePaths)
{
File.Delete(filePath);
}
foreach (string folderPath in folderPaths)
{
Directory.Delete(folderPath, true);
}
}
else
{
Console.WriteLine(“Please enter Valid publish and build path”);
}
Engine objEngine = new Engine();
objEngine.BinPath = @”C:\Windows\Microsoft.NET\Framework\v4.0.30319″;
FileLogger logger = new FileLogger();
logger.Parameters = @”logfile=C:\temp\build.log”;
objEngine.RegisterLogger(logger);
BuildPropertyGroup objBuildPropertyGroup = new BuildPropertyGroup();
objBuildPropertyGroup.SetProperty(“Configuration”, “Release”);
objBuildPropertyGroup.SetProperty(“OutputPath”, strBuildPath);
bool success = objEngine.BuildProjectFile(solutionFilePath, new string[] { “Rebuild” }, objBuildPropertyGroup);
try
{
string[] pathDir = solutionFilePath.Split(‘\\’);
string folderName = pathDir[pathDir.Length – 1].Replace(“csproj”, “”);
string strPublishPath = Path.Combine(strBuildPath, “_PublishedWebsites”, folderName);
string[] filePaths = Directory.GetFiles(strBuildPath);
string[] publishedFilePaths = Directory.GetFiles(strPublishPath);
string[] publishedFolderPaths = Directory.GetDirectories(strPublishPath);
foreach (string filePath in filePaths)
{
File.Delete(filePath);
}
foreach (string fPath in publishedFilePaths)
{
File.Move(fPath, Path.Combine(strBuildPath, Path.GetFileName(fPath)));
}
foreach (string dirPath in publishedFolderPaths)
{
Directory.Move(dirPath, Path.Combine(strBuildPath, new DirectoryInfo(dirPath).Name));
}
Directory.Delete(Path.Combine(strBuildPath, “_PublishedWebsites”), true);
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
success = false;
}
if (success)
{
Console.WriteLine(“Publishing Completed Successfully”);
}
else
{
Console.WriteLine(“Publishing Failed”);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
————————————————————