在过去的几天中,我一直在阅读这篇文章,并且似乎有很多关于如何创建Web安装项目并使用自定义操作修改它的信息。到目前为止,这种使用以下链接:如何修改Web安装项目以创建新网站,应用程序池和虚拟目录?
http://weblogs.asp.net/scottgu/tip-trick-creating-packaged-asp-net-setup-programs-with-vs-2005 http://msdn.microsoft.com/en-us/library/ms525598.aspx http://www.dreamincode.net/forums/topic/231074-setup-and-deployment-in-visual-studio-2010/ http://www.codeproject.com/Articles/146626/Extending-Visual-Studio-Setup-Project
然而,似乎没有太大的如何具体修改用户界面,使用户可以创建一个新的网站,而不是从下拉列表中选择在“安装地址”操作中(在“开始”下)提供。除此之外,我还遇到过有关如何创建新应用程序池的代码,但不知道如何阻止UI让用户选择从列表中首先选择1。
此刻我的安装程序看起来很喜欢这样的:
正如你可以看到该用户目前有3场;网站,虚拟目录和应用程序池。网站和应用程序池是其中包含现有选项的列表。在网站下拉列表的情况下,我真的希望这是一个文本框,允许用户输入他们希望创建的网站的名称。对于应用程序池,我希望它完全消失,因为我的自定义操作会自动创建应用程序池并为其分配虚拟目录(希望)。
我已经有了用于创建应用程序池,虚拟目录并将其分配给应用程序池的代码(尚未测试),但我无法看到如何编辑用户界面以满足我的需求。我是否需要删除“安装地址”步骤并创建我自己的定制?
下面代码的应用程序池的创建,虚拟目录创建和虚拟目录分配到应用程序池:
//Properties for user input
private string targetSite { get { return this.Context.Parameters["targetsite"]; }}
private string targetVirtualDir { get { return this.Context.Parameters["targetvdir"]; } }
private string targetDirectory { get { return this.Context.Parameters["targetdir"]; } }
private const string ApplicationPool = "TestWebAppAP";
private const string BasePath = "IIS://Localhost/W3SVC/1/Root";
private const string AppPoolsAddress = "IIS://Localhost/W3SVC/AppPools";
//Install
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
if (targetSite == null) throw new InstallException("IIS site name not specified");
else
{
CreateApplicationPool();
CreateVirtualDir();
AssignVDirtoAppPool();
}
}
//Create Application Pool
private void CreateApplicationPool()
{
try
{
DirectoryEntry NewPool;
DirectoryEntry AppPools = new DirectoryEntry(AppPoolsAddress);
NewPool = AppPools.Children.Add(ApplicationPool, "IIsApplicationPool");
NewPool.CommitChanges();
}
catch (Exception ex)
{
MessageBox.Show("Failed to create new application pool \" " + ApplicationPool + "\". Error: " + ex.Message);
throw new InstallException("Failed to create app pool", ex);
}
}
//Create Virtual Directory
private void CreateVirtualDir()
{
try
{
DirectoryEntry site = new DirectoryEntry(BasePath);
string className = site.SchemaClassName.ToString();
if (className.EndsWith("Server") || className.EndsWith("VirtualDir"))
{
DirectoryEntries vdirs = site.Children;
DirectoryEntry newVDir = vdirs.Add(targetVirtualDir, (className.Replace("Service", "VirtualDir")));
newVDir.Properties["Path"][0] = targetDirectory;
newVDir.Properties["AccessScript"][0] = true;
newVDir.Properties["AppFriendlyName"][0] = targetVirtualDir;
newVDir.Properties["AppIsolated"][0] = "1";
newVDir.Properties["AppRoot"][0] = "/LM" + BasePath.Substring(BasePath.IndexOf("/", ("IIS://".Length)));
newVDir.CommitChanges();
}
else
{
MessageBox.Show("Failed to create virtual directory. It can only be created in a site or virtual directory node.");
}
}
catch (Exception ex)
{
MessageBox.Show("Failed to create virtual directory \"" + targetVirtualDir + "\". Error: " + ex.Message);
throw new InstallException("Failed to create virtual directory", ex);
}
}
//Assign virtual directory to app pool
private void AssignVDirtoAppPool()
{
try
{
DirectoryEntry vDir = new DirectoryEntry(BasePath);
string className = vDir.SchemaClassName.ToString();
if(className.EndsWith("VirtualDir"))
{
object[] param = { 0, ApplicationPool, true };
vDir.Invoke("AppCreate3", param);
vDir.Properties["AppIsolated"][0] = "2";
}
else
{
MessageBox.Show("Failed to assign to application pool. Only virtual directories can be assigned to application pools.");
}
}
catch (Exception ex)
{
MessageBox.Show("Failed to assign virtual directory \"" + targetVirtualDir + "\" to application pool \"" + ApplicationPool + "\". Error: " + ex.Message);
throw new InstallException("Failed to assign virtual directory to application pool", ex);
}
}
我创建了一个Web安装程序,以便将我的网站部署到IIS,但奇怪的是站点下拉和应用程序池下拉不显示在安装程序中。他们是否默认存在,或者我们需要为安装向导上显示的这些下拉菜单做些特别的事情? – user2913184
@ user2913184如果他们不在那里,你将不得不创建一个自定义的。 – sr28
事实上,这几乎是我的想法,尽管感谢您的帮助。 – user2913184