如何用c++重写URL创建IIS站点?



我目前正在使用c++创建IIS站点,代码如下。我想设置这个网站的服务器变量和重写URL。有人能帮我做这件事吗?提前谢谢。

// IADs and IADsContainer is from C:Program Files (x86)Windows Kits8.1IncludeumIads.h
CComPtr< IADs > spADsWebServer;
CComPtr< IADsContainer > spADsContainer;
M_HR( ADsGetObject( 
L"IIS://localhost/w3svc", 
IID_IADsContainer, OUT ( void** )&spADsContainer ) );
CComBSTR bstrIndex( {UNIQUE SITE ID} );
CComPtr< IDispatch > spIDispatchWebServer;
M_HR( spADsContainer->Create( IIS_CLASS_WEB_SERVER_W, 
bstrIndex, OUT &spIDispatchWebServer ) );
M_HR( spIDispatchWebServer->QueryInterface( 
IID_IADs, OUT ( void** )&spADsWebServer ) );
CString szIPAddress;
CString szHostName;
vector< CString > vecBindings( 1 );
vecBindings[ 0 ] = szIPAddress + 
L":" + CString( pwszPort ) + 
L":" + szHostName;
CMComVariant mvarBindings; 
M_HR( VariantHelper::MakeSafeArray( vecBindings, OUT &mvarBindings ) );
M_HR( spADsWebServer->Put( L"ServerBindings", mvarBindings ) );
M_HR( pIADsWebServer->Put( L"ServerComment", CMComVariant( pwszComment ) ) );
// Specify the default document.
M_HR( pIADsWebServer->Put( L"DefaultDoc", CMComVariant( pwszDefaultDoc ) ) );

// MY CODE LOGIC TO CREATE VIRTUAL DIRECTORY 

你可以试试下面的c++代码:

添加网站:

//.h file code:
using namespace Microsoft::Web::Administration;
class Sample final
{
public:
static void Main();
};

int main(int argc, char **argv)
{
Sample::Main();
}
//.cpp file code:
#include "snippet.h"
using namespace Microsoft::Web::Administration;
void Sample::Main()
{
//ORIGINAL LINE: using(ServerManager serverManager = new ServerManager())
{
ServerManager serverManager = ServerManager();
Configuration *config = serverManager.GetApplicationHostConfiguration();
ConfigurationSection *sitesSection = config->GetSection(L"system.applicationHost/sites");
ConfigurationElementCollection *sitesCollection = sitesSection->GetCollection();
ConfigurationElement *siteElement = sitesCollection->CreateElement(L"site");
siteElement[L"name"] = LR"(test)";
ConfigurationElementCollection *bindingsCollection = siteElement->GetCollection(L"bindings");
ConfigurationElement *bindingElement = bindingsCollection->CreateElement(L"binding");
bindingElement[L"protocol"] = LR"(89)";
bindingsCollection->Add(bindingElement);
ConfigurationElement *applicationDefaultsElement = siteElement->GetChildElement(L"applicationDefaults");
applicationDefaultsElement[L"path"] = LR"(C:myapp)";
applicationDefaultsElement[L"applicationPool"] = LR"(test)";
sitesCollection->Add(siteElement);
serverManager.CommitChanges();
}
}

添加URL重写规则:

//.h file code:
using namespace Microsoft::Web::Administration;
class Sample final
{
public:
static void Main();
};

int main(int argc, char **argv)
{
Sample::Main();
}
//.cpp file code:
#include "snippet.h"
using namespace Microsoft::Web::Administration;
void Sample::Main()
{

//ORIGINAL LINE: using(ServerManager serverManager = new ServerManager())
{
ServerManager serverManager = ServerManager();
Configuration *config = serverManager.GetWebConfiguration(L"test");
ConfigurationSection *rulesSection = config->GetSection(L"system.webServer/rewrite/rules");
ConfigurationElementCollection *rulesCollection = rulesSection->GetCollection();
ConfigurationElement *ruleElement = rulesCollection->CreateElement(L"rule");
ruleElement[L"name"] = LR"(redirect rule)";
ConfigurationElement *conditionsElement = ruleElement->GetChildElement(L"conditions");
ConfigurationElementCollection *conditionsCollection = conditionsElement->GetCollection();
ConfigurationElement *addElement = conditionsCollection->CreateElement(L"add");
addElement[L"input"] = LR"({REQUEST_URI})";
addElement[L"pattern"] = LR"(^/test/(.*))";
conditionsCollection->Add(addElement);
ConfigurationElement *actionElement = ruleElement->GetChildElement(L"action");
actionElement[L"type"] = LR"(Redirect)";
actionElement[L"url"] = LR"(https://www.example.com/{C:1})";
actionElement[L"logRewrittenUrl"] = true;
rulesCollection->Add(ruleElement);
serverManager.CommitChanges();
}
}

最新更新