Programatically add new website with proper binding info into iis using c#.
Here i am writing an article for Creating website in iis using c#.
For Adding or deleting website from IIS here is this scenareo I must say IIS7 is the best friend for the developers.Because administering websites through code was made very simple by Microsoft.Web.Administration.dll which is present in the %windir%\system32\inetsrv folder.IS 7.0 and above provide a comprehensive managed-code management application programming interface (API) that allows complete manipulation of the XML configuration files and convenience access to server objects.
Intro:
IIS includes Microsoft.Web.Administration, which is a new a management API for the web server that enables editing configuration through complete manipulation of the XML configuration files. It also provides convenience objects to manage the server, its properties and state. The configuration editing aspect of the API provides programmatic access to read and write configuration properties in the IIS configuration file hierarchy and specific configuration files. The object management aspect of this API provides a series of top-level administration objects for direct management of the server (i.e. sites, application pools, worker processes, etc).
The ServerManager is the factory class that contains a set of server convenience objects to which properties and methods are available to use in a strongly-type way. It is the main entry point for managing the server. Managing the server could have been done via other cumbersome routes (accessing raw configuration XML or calling state APIs), but through these objects managing the server is seamless.The most common set of objects are available to use via the server manager include: applications, virtual directories, sites, worker processes and application domains
ServerManager serverManager = new ServerManager();
The sites object enables access to a sites properties and applications. It also contains methods to add a site to the system or get the total site count. The add method also defines the name of the site, the root virtual directory path,
and the port number as integer.
Note: Add Microsoft.web.Administration dll.
Environment: IIS 7
Code : Page Default.apsx.cs
using System;Conclusion:
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Web.Administration;
using System.Drawing;
namespace AddWebsiteIIS7
{
public partial class _Default : System.Web.UI.Page
{
ServerManager serverMgr = new ServerManager();
protected void Page_Load(object sender, EventArgs e)
{
lblmsg.Text = "";
}
protected void btncreate_Click(object sender, EventArgs e)
{
try
{ string strWebsitename = txtwebsitename.Text; // abc
string strApplicationPool = "DefaultAppPool"; // set your deafultpool :4.0 in IIS
string strhostname = txthostname.Text; //abc.com
string stripaddress = txtipaddress.Text;// ip address
string bindinginfo = stripaddress + ":80:" + strhostname;
//check if website name already exists in IIS
Boolean bWebsite = IsWebsiteExists(strWebsitename);
if (!bWebsite)
{
Site mySite = serverMgr.Sites.Add(strWebsitename.ToString(), "http", bindinginfo, "C:\\inetpub\\wwwroot\\yourWebsite");
mySite.ApplicationDefaults.ApplicationPoolName = strApplicationPool;
mySite.TraceFailedRequestsLogging.Enabled = true;
mySite.TraceFailedRequestsLogging.Directory = "C:\\inetpub\\customfolder\\site";
serverMgr.CommitChanges();
lblmsg.Text = "New website " + strWebsitename + " added sucessfully";
lblmsg.ForeColor = System.Drawing.Color.Green;
}
else{
lblmsg.Text = "Name should be unique, " + strWebsitename + " is already exists. ";
lblmsg.ForeColor = System.Drawing.Color.Red;
}
}
catch (Exception ae)
{
Response.Redirect(ae.Message);
}
}
public bool IsWebsiteExists(string strWebsitename)
{
Boolean flagset = false;
SiteCollection sitecollection = serverMgr.Sites;
foreach (Site site in sitecollection)
{
if (site.Name == strWebsitename.ToString())
{
flagset = true;
break;
}
else{
flagset = false;
}
}
return flagset;
}
}
}
This article demonstrated one of the concepts to create a new website on IIS programmatically using ASP.NET and C#.
Note:
- Make sure your redirection.config file has Read,Write full Permission.
- It is just my experienced which is explained in the above article also it doesn’t include additional things like exception handling & web service security. Please feel to modify it according to your requirements.
Không có nhận xét nào:
Đăng nhận xét