以编程方式创建具有不同域名和不同虚拟机的 asp 网络应用程序的多个实例
Programmatically creating multiple instances of asp web app with different domain names and in different Virtual Machines
我创建了一个新的 ASP.NET MVC 网络应用程序,它负责学校管理。现在我想创建新的 Web 应用程序,以帮助使用新的子域和新注册客户端的新数据库部署此应用程序。
完成这项工作的最佳方法是什么?
我对这类应用程序的信息很少,根据这些信息,我认为它与网络托管公司提供的服务有关,例如 Microsoft Azure,它以编程方式提供虚拟机的创建和配置,并能够将应用程序部署到这个新的还以编程方式创建了虚拟机!我在写吗?!
如果您仍然不明白我的问题,请尝试访问 shopify (https://www.shopify.com/),它以我在项目中想要的相同方式为非专业人士提供商店创建。
谢谢
下面的代码在 IIS 主机上创建了一个新的虚拟目录。
/// <summary>
/// Create a new virtual directory on the iis host.
/// </summary>
/// <param name="iisHostPath">The iis host path.</param>
/// <param name="physicalPath">The physical path to the directory.</param>
/// <param name="virtualDirectoryName">The virtual directory name.</param>
/// <param name="defaultDocument">The defualt document to set.</param>
/// <returns>True if the virtual directory was created else false.</returns>
/// <example>
/// iisHostPath : [servername]/[service]/[websiteID]/[Root] : localhost/W3SVC/1/Root
/// defaultDocument : [document] : default.aspx
/// </example>
/// <remarks>
/// <para>iisHostPath : [servername]/[service]/[websiteID]/[Root] : localhost/W3SVC/1/Root</para>
/// <para>defaultDocument : [document] : default.aspx</para>
/// </remarks>
public virtual bool CreateVirtualDirectory(string iisHostPath, string physicalPath,
string virtualDirectoryName, string defaultDocument)
{
// Validate the inputs.
if (String.IsNullOrEmpty(iisHostPath))
throw new System.ArgumentNullException("IIS path can not be null.",
new System.Exception("A valid IIS path should be specified."));
// Validate the inputs.
if (String.IsNullOrEmpty(physicalPath))
throw new System.ArgumentNullException("Physical can not be null.",
new System.Exception("A valid physical path should be specified."));
// Validate the inputs.
if (String.IsNullOrEmpty(virtualDirectoryName))
throw new System.ArgumentNullException("Virtual directory name can not be null.",
new System.Exception("A valid virtual directory name should be specified."));
// Validate the inputs.
if (String.IsNullOrEmpty(defaultDocument))
throw new System.ArgumentNullException("Default document can not be null.",
new System.Exception("A valid default document should be specified."));
// Create a new directory entry
// instance to the iis machine.
DirectoryEntry localMachine = new DirectoryEntry(
"IIS://" + iisHostPath);
// Add the iis virtual directory
// to the iis collection.
DirectoryEntry virtName = localMachine.Children.Add(virtualDirectoryName, "IIsWebVirtualDir");
// Commit the changes for the account.
virtName.CommitChanges();
// Assign default properties.
virtName.Properties["Path"][0] = physicalPath;
virtName.Properties["DefaultDoc"][0] = defaultDocument;
virtName.Properties["AccessScript"][0] = true;
// These properties are necessary for an application to be created.
virtName.Properties["AppFriendlyName"][0] = virtualDirectoryName;
virtName.Properties["AppIsolated"][0] = "1";
virtName.Properties["AppRoot"][0] = "/LM/" + iisHostPath;
// Commit the changes for the account.
virtName.CommitChanges();
// Close the connections.
virtName.Close();
localMachine.Close();
// Return success.
return true;
}
下面的代码在 II 主机上创建一个新网站。
/// <summary>
/// Create a new web site on the iis host.
/// </summary>
/// <param name="iisHostPath">The iis host path.</param>
/// <param name="websiteID">The unique web site id.</param>
/// <param name="websiteName">The name of the web site.</param>
/// <param name="physicalPath">The physical path to the root directory.</param>
/// <returns>True if the web site was created else false.</returns>
/// <example>
/// iisHostPath : [servername]/[service] : localhost/W3SVC
/// websiteID : [number] : 454354
/// </example>
/// <remarks>
/// <para>iisHostPath : [servername]/[service] : localhost/W3SVC</para>
/// <para>websiteID : [number] : 454354</para>
/// </remarks>
public virtual bool CreateWebSite(string iisHostPath,
string websiteID, string websiteName, string physicalPath)
{
// Validate the inputs.
if (String.IsNullOrEmpty(iisHostPath))
throw new System.ArgumentNullException("IIS path can not be null.",
new System.Exception("A valid IIS path should be specified."));
// Validate the inputs.
if (String.IsNullOrEmpty(websiteID))
throw new System.ArgumentNullException("Web site id can not be null.",
new System.Exception("A valid web site id should be specified."));
// Validate the inputs.
if (String.IsNullOrEmpty(websiteName))
throw new System.ArgumentNullException("Web site name can not be null.",
new System.Exception("A valid web site name should be specified."));
// Validate the inputs.
if (String.IsNullOrEmpty(physicalPath))
throw new System.ArgumentNullException("Physical can not be null.",
new System.Exception("A valid physical path should be specified."));
// Create a new directory entry
// instance to the iis machine.
DirectoryEntry localMachine = new DirectoryEntry(
"IIS://" + iisHostPath);
// Add the iis web site
// to the iis collection.
DirectoryEntry siteName = localMachine.Children.Add(websiteID, "IIsWebServer");
// Assign the web site properties.
siteName.Properties["ServerComment"][0] = websiteName;
siteName.CommitChanges();
// Commit the changes for the account.
siteName.CommitChanges();
// Add the iis web site
// to the iis collection.
DirectoryEntry rootName = siteName.Children.Add("Root", "IIsWebVirtualDir");
// Assign the web site properties.
rootName.Properties["Path"][0] = physicalPath;
rootName.Properties["AccessScript"][0] = true;
// Commit the changes for the account.
rootName.CommitChanges();
// Close the connections.
rootName.Close();
siteName.Close();
localMachine.Close();
// Return success.
return true;
}
下面的代码设置了 IIS 主机上网站的端口号。
/// <summary>
/// Set a port number to a web site on the iis host.
/// </summary>
/// <param name="iisHostPath">The iis host path.</param>
/// <param name="portNumber">The port number.</param>
/// <returns>True if the port number was assigned else false.</returns>
/// <example>
/// iisHostPath : [servername]/[service]/[websiteID] : localhost/W3SVC/1
/// </example>
/// <remarks>
/// <para>iisHostPath : [servername]/[service]/[websiteID] : localhost/W3SVC/1</para>
/// </remarks>
public virtual bool SetWebSitePortNumber(string iisHostPath, int portNumber)
{
// Validate the inputs.
if (String.IsNullOrEmpty(iisHostPath))
throw new System.ArgumentNullException("IIS path can not be null.",
new System.Exception("A valid IIS path should be specified."));
// Validate the inputs.
if (portNumber < 1)
throw new System.ArgumentNullException("Port number not valid.",
new System.Exception("The port number must be greater than zero."));
// Create a new directory entry
// instance to the iis machine.
DirectoryEntry localMachine = new DirectoryEntry(
"IIS://" + iisHostPath);
// Set the web site port number.
localMachine.Properties["ServerBindings"][0] = ":" + portNumber + ":";
// Commit the changes for the account.
localMachine.CommitChanges();
// Close the connections.
localMachine.Close();
// Return success.
return true;
}
请注意,localMachine.Properties["ServerBindings"][0] =
指的是 Bindings
在您网站的 Advanced Settings
中的 IIS。
我创建了一个新的 ASP.NET MVC 网络应用程序,它负责学校管理。现在我想创建新的 Web 应用程序,以帮助使用新的子域和新注册客户端的新数据库部署此应用程序。 完成这项工作的最佳方法是什么? 我对这类应用程序的信息很少,根据这些信息,我认为它与网络托管公司提供的服务有关,例如 Microsoft Azure,它以编程方式提供虚拟机的创建和配置,并能够将应用程序部署到这个新的还以编程方式创建了虚拟机!我在写吗?! 如果您仍然不明白我的问题,请尝试访问 shopify (https://www.shopify.com/),它以我在项目中想要的相同方式为非专业人士提供商店创建。 谢谢
下面的代码在 IIS 主机上创建了一个新的虚拟目录。
/// <summary>
/// Create a new virtual directory on the iis host.
/// </summary>
/// <param name="iisHostPath">The iis host path.</param>
/// <param name="physicalPath">The physical path to the directory.</param>
/// <param name="virtualDirectoryName">The virtual directory name.</param>
/// <param name="defaultDocument">The defualt document to set.</param>
/// <returns>True if the virtual directory was created else false.</returns>
/// <example>
/// iisHostPath : [servername]/[service]/[websiteID]/[Root] : localhost/W3SVC/1/Root
/// defaultDocument : [document] : default.aspx
/// </example>
/// <remarks>
/// <para>iisHostPath : [servername]/[service]/[websiteID]/[Root] : localhost/W3SVC/1/Root</para>
/// <para>defaultDocument : [document] : default.aspx</para>
/// </remarks>
public virtual bool CreateVirtualDirectory(string iisHostPath, string physicalPath,
string virtualDirectoryName, string defaultDocument)
{
// Validate the inputs.
if (String.IsNullOrEmpty(iisHostPath))
throw new System.ArgumentNullException("IIS path can not be null.",
new System.Exception("A valid IIS path should be specified."));
// Validate the inputs.
if (String.IsNullOrEmpty(physicalPath))
throw new System.ArgumentNullException("Physical can not be null.",
new System.Exception("A valid physical path should be specified."));
// Validate the inputs.
if (String.IsNullOrEmpty(virtualDirectoryName))
throw new System.ArgumentNullException("Virtual directory name can not be null.",
new System.Exception("A valid virtual directory name should be specified."));
// Validate the inputs.
if (String.IsNullOrEmpty(defaultDocument))
throw new System.ArgumentNullException("Default document can not be null.",
new System.Exception("A valid default document should be specified."));
// Create a new directory entry
// instance to the iis machine.
DirectoryEntry localMachine = new DirectoryEntry(
"IIS://" + iisHostPath);
// Add the iis virtual directory
// to the iis collection.
DirectoryEntry virtName = localMachine.Children.Add(virtualDirectoryName, "IIsWebVirtualDir");
// Commit the changes for the account.
virtName.CommitChanges();
// Assign default properties.
virtName.Properties["Path"][0] = physicalPath;
virtName.Properties["DefaultDoc"][0] = defaultDocument;
virtName.Properties["AccessScript"][0] = true;
// These properties are necessary for an application to be created.
virtName.Properties["AppFriendlyName"][0] = virtualDirectoryName;
virtName.Properties["AppIsolated"][0] = "1";
virtName.Properties["AppRoot"][0] = "/LM/" + iisHostPath;
// Commit the changes for the account.
virtName.CommitChanges();
// Close the connections.
virtName.Close();
localMachine.Close();
// Return success.
return true;
}
下面的代码在 II 主机上创建一个新网站。
/// <summary>
/// Create a new web site on the iis host.
/// </summary>
/// <param name="iisHostPath">The iis host path.</param>
/// <param name="websiteID">The unique web site id.</param>
/// <param name="websiteName">The name of the web site.</param>
/// <param name="physicalPath">The physical path to the root directory.</param>
/// <returns>True if the web site was created else false.</returns>
/// <example>
/// iisHostPath : [servername]/[service] : localhost/W3SVC
/// websiteID : [number] : 454354
/// </example>
/// <remarks>
/// <para>iisHostPath : [servername]/[service] : localhost/W3SVC</para>
/// <para>websiteID : [number] : 454354</para>
/// </remarks>
public virtual bool CreateWebSite(string iisHostPath,
string websiteID, string websiteName, string physicalPath)
{
// Validate the inputs.
if (String.IsNullOrEmpty(iisHostPath))
throw new System.ArgumentNullException("IIS path can not be null.",
new System.Exception("A valid IIS path should be specified."));
// Validate the inputs.
if (String.IsNullOrEmpty(websiteID))
throw new System.ArgumentNullException("Web site id can not be null.",
new System.Exception("A valid web site id should be specified."));
// Validate the inputs.
if (String.IsNullOrEmpty(websiteName))
throw new System.ArgumentNullException("Web site name can not be null.",
new System.Exception("A valid web site name should be specified."));
// Validate the inputs.
if (String.IsNullOrEmpty(physicalPath))
throw new System.ArgumentNullException("Physical can not be null.",
new System.Exception("A valid physical path should be specified."));
// Create a new directory entry
// instance to the iis machine.
DirectoryEntry localMachine = new DirectoryEntry(
"IIS://" + iisHostPath);
// Add the iis web site
// to the iis collection.
DirectoryEntry siteName = localMachine.Children.Add(websiteID, "IIsWebServer");
// Assign the web site properties.
siteName.Properties["ServerComment"][0] = websiteName;
siteName.CommitChanges();
// Commit the changes for the account.
siteName.CommitChanges();
// Add the iis web site
// to the iis collection.
DirectoryEntry rootName = siteName.Children.Add("Root", "IIsWebVirtualDir");
// Assign the web site properties.
rootName.Properties["Path"][0] = physicalPath;
rootName.Properties["AccessScript"][0] = true;
// Commit the changes for the account.
rootName.CommitChanges();
// Close the connections.
rootName.Close();
siteName.Close();
localMachine.Close();
// Return success.
return true;
}
下面的代码设置了 IIS 主机上网站的端口号。
/// <summary>
/// Set a port number to a web site on the iis host.
/// </summary>
/// <param name="iisHostPath">The iis host path.</param>
/// <param name="portNumber">The port number.</param>
/// <returns>True if the port number was assigned else false.</returns>
/// <example>
/// iisHostPath : [servername]/[service]/[websiteID] : localhost/W3SVC/1
/// </example>
/// <remarks>
/// <para>iisHostPath : [servername]/[service]/[websiteID] : localhost/W3SVC/1</para>
/// </remarks>
public virtual bool SetWebSitePortNumber(string iisHostPath, int portNumber)
{
// Validate the inputs.
if (String.IsNullOrEmpty(iisHostPath))
throw new System.ArgumentNullException("IIS path can not be null.",
new System.Exception("A valid IIS path should be specified."));
// Validate the inputs.
if (portNumber < 1)
throw new System.ArgumentNullException("Port number not valid.",
new System.Exception("The port number must be greater than zero."));
// Create a new directory entry
// instance to the iis machine.
DirectoryEntry localMachine = new DirectoryEntry(
"IIS://" + iisHostPath);
// Set the web site port number.
localMachine.Properties["ServerBindings"][0] = ":" + portNumber + ":";
// Commit the changes for the account.
localMachine.CommitChanges();
// Close the connections.
localMachine.Close();
// Return success.
return true;
}
请注意,localMachine.Properties["ServerBindings"][0] =
指的是 Bindings
在您网站的 Advanced Settings
中的 IIS。