如何在C++中使用URL重写创建IIS网站?

发布于 2025-01-22 00:40:58 字数 1422 浏览 3 评论 0原文

我目前正在使用C ++创建IIS网站,其中包括以下代码。我想设置服务器变量并重写此站点的URL。谁能帮我这样做?提前致谢。

    // IADs and IADsContainer is from C:\Program Files (x86)\Windows Kits\8.1\Include\um\Iads.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 

I am currently creating IIS site using c++, with below code. I would like to set the server variables and Rewrite URL for this site. Can anyone help me to do this? Thanks in advance.

    // IADs and IADsContainer is from C:\Program Files (x86)\Windows Kits\8.1\Include\um\Iads.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 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪 2025-01-29 00:40:58

您可以尝试以下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();
    }
}

you could try the below c++ code:

add site:

//.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();
    }
}

To add URL rewrite rule:

//.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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文