C#在asp.net项目中用程序在IIS中创建站点
本文关键字:IIS 创建 站点 程序 net 项目 asp | 更新日期: 2023-09-27 18:20:19
我正试图使用C#中的Microsoft.Web.Administration dll在IIS中创建一个网站。我正在使用以下代码
using (var serverManager = new ServerManager())
{
ApplicationPool pool = serverManager.ApplicationPools.Add(ClientDomain);
pool.ManagedRuntimeVersion = "v4.0";
Site site = serverManager.Sites.Add(SiteName, httpProtocol, httpBindingInformation, physicalPath);
site.Bindings.Add(httpsBindingInformation, httpsProtocol);
serverManager.CommitChanges();
}
当我从asp.net项目中运行代码时,我会遇到以下异常"方法或操作未实现",但如果从控制台项目中运行相同的代码,它会正常工作。以下是异常的详细信息
at Microsoft.Web.Administration.Interop.IAppHostProperty.get_Value()
at Microsoft.Web.Administration.ConfigurationElement.GetPropertyValue(IAppHostProperty property)
at Microsoft.Web.Administration.ConfigurationElement.GetAttributeValue(String attributeName)
at Microsoft.Web.Administration.Binding.get_CertificateHash()
at Microsoft.Web.Administration.BindingCollection.Add(Binding binding)
at Microsoft.Web.Administration.BindingCollection.Add(String bindingInformation, String bindingProtocol)
at TestProject.BLL.CRMSiteManagement.CRMSiteIISSetup.Process() in e:'CRMSiteManagement'CRMSiteIISSetup.cs:line 35
at TestProject.BLL.CRMSiteManagement.CRMSiteService.CreateNewCRMSite(CRMSite newSite) in e:CRMSiteManagement'CRMSiteService.cs:line
这似乎是一个权利问题,但我不知道如何解决。更新1:localhost 上的以下代码行出现错误
site.Bindings.Add(httpsBindingInformation, httpsProtocol);
我认为您试图设置两次相同的绑定:
- 通过调用Add方法一次
Site site = serverManager.Sites.Add(SiteName, httpProtocol, httpBindingInformation, physicalPath);
- 然后再次添加相同的绑定:
site.Bindings.Add(httpsBindingInformation, httpsProtocol);