日韩欧美人妻无码精品白浆,夜夜嗨AV免费入口,国产欧美官网在线看,高校回应聋哑女生因长相完美被质疑

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發(fā)文檔 其他文檔  
 
網(wǎng)站管理員

【C#】自動檢測Windows Server服務(wù)器是否已經(jīng)安裝并配置IIS管理器的ASP和ASP.NET(.aspx)支持代碼

admin
2025年6月11日 9:19 本文熱度 114

以下是完整的C#代碼,用于Windows Server服務(wù)器自動安裝并配置IIS的ASP(經(jīng)典ASP)和ASP.NET(.aspx)支持。代碼包含功能檢測,只有在未安裝時才執(zhí)行安裝操作:

using System;

using System.Diagnostics;

using Microsoft.Web.Administration;


class IISFullSupportInstaller

{

    static void Main()

    {

        try

        {

            Console.WriteLine("正在檢測IIS功能安裝狀態(tài)...");

            

            bool needRestart = false;

            

            // 安裝缺失的功能

            if (!IsASPInstalled())

            {

                Console.WriteLine("檢測到經(jīng)典ASP功能未安裝");

                InstallIISFeature("IIS-ASP");

                needRestart = true;

            }

            

            if (!IsASPNETInstalled())

            {

                Console.WriteLine("檢測到ASP.NET功能未安裝");

                InstallIISFeature("IIS-ASPNET45");

                needRestart = true;

            }

            

            // 配置IIS

            if (needRestart)

            {

                Console.WriteLine("需要重啟后繼續(xù)配置,按任意鍵重啟計算機...");

                Console.ReadKey();

                RestartComputer();

            }

            else

            {

                ConfigureIIS();

                Console.WriteLine("ASP和ASP.NET支持已成功配置!");

            }

        }

        catch (Exception ex)

        {

            Console.WriteLine($"操作失敗: {ex.Message}");

            Console.WriteLine("請以管理員身份運行此程序");

        }

    }


    // 檢查經(jīng)典ASP是否已安裝

    static bool IsASPInstalled()

    {

        using (ServerManager serverManager = new ServerManager())

        {

            try

            {

                // 嘗試獲取ASP配置部分,如果拋出異常說明未安裝

                var aspSection = serverManager.GetApplicationHostConfiguration().GetSection("system.webServer/asp");

                return true;

            }

            catch

            {

                return false;

            }

        }

    }


    // 檢查ASP.NET是否已安裝

    static bool IsASPNETInstalled()

    {

        using (ServerManager serverManager = new ServerManager())

        {

            try

            {

                // 檢查.NET Framework處理程序是否存在

                var config = serverManager.GetApplicationHostConfiguration();

                var runtimeSection = config.GetSection("system.webServer/runtime");

                return true;

            }

            catch

            {

                return false;

            }

        }

    }


    // 安裝IIS功能

    static void InstallIISFeature(string featureName)

    {

        ProcessStartInfo startInfo = new ProcessStartInfo

        {

            FileName = "dism.exe",

            Arguments = $"/Online /Enable-Feature /FeatureName:{featureName} /All /NoRestart",

            RedirectStandardOutput = true,

            RedirectStandardError = true,

            UseShellExecute = false,

            CreateNoWindow = true

        };


        using (Process process = new Process { StartInfo = startInfo })

        {

            Console.WriteLine($"正在安裝 {featureName} ...");

            process.Start();

            string output = process.StandardOutput.ReadToEnd();

            string error = process.StandardError.ReadToEnd();

            process.WaitForExit();


            if (process.ExitCode != 0)

            {

                throw new Exception($"{featureName} 安裝失敗 (代碼 {process.ExitCode}): {error}");

            }

            

            if (!output.Contains("操作成功完成"))

            {

                throw new Exception($"{featureName} 安裝未完成: " + output);

            }

            

            Console.WriteLine($"{featureName} 安裝成功");

        }

    }


    // 配置IIS支持ASP和ASP.NET

    static void ConfigureIIS()

    {

        using (ServerManager serverManager = new ServerManager())

        {

            Console.WriteLine("正在配置IIS支持...");

            

            // 1. 配置經(jīng)典ASP支持

            if (!IsASPConfigured(serverManager))

            {

                Console.WriteLine("配置經(jīng)典ASP支持");

                ConfigureClassicASP(serverManager);

            }

            

            // 2. 配置ASP.NET支持

            if (!IsASPNETConfigured(serverManager))

            {

                Console.WriteLine("配置ASP.NET支持");

                ConfigureASPNET(serverManager);

            }

            

            // 3. 保存所有更改

            serverManager.CommitChanges();

            Console.WriteLine("配置保存成功");

        }

    }


    // 檢查經(jīng)典ASP是否已配置

    static bool IsASPConfigured(ServerManager serverManager)

    {

        try

        {

            var config = serverManager.GetApplicationHostConfiguration();

            var restrictions = config.GetSection("system.webServer/security/isapiCgiRestriction").GetCollection();

            

            foreach (var item in restrictions)

            {

                if (item["path"].ToString().EndsWith("asp.dll", StringComparison.OrdinalIgnoreCase) && 

                    (bool)item["allowed"])

                {

                    return true;

                }

            }

            return false;

        }

        catch

        {

            return false;

        }

    }


    // 配置經(jīng)典ASP

    static void ConfigureClassicASP(ServerManager serverManager)

    {

        var config = serverManager.GetApplicationHostConfiguration();

        

        // 啟用ASP ISAPI擴展

        var isapiSection = config.GetSection("system.webServer/security/isapiCgiRestriction");

        var restrictions = isapiSection.GetCollection();

        

        ConfigurationElement aspExtension = restrictions.CreateElement();

        aspExtension["path"] = @"%windir%\system32\inetsrv\asp.dll";

        aspExtension["allowed"] = true;

        aspExtension["description"] = "ASP Classic Support";

        restrictions.Add(aspExtension);


        // 配置ASP全局設(shè)置

        ConfigurationSection aspSection = config.GetSection("system.webServer/asp");

        aspSection["enableParentPaths"] = true;

        aspSection["bufferingOn"] = true;

        aspSection["scriptErrorSentToBrowser"] = true;


        // 為默認網(wǎng)站添加ASP處理程序映射

        Configuration webConfig = serverManager.GetWebConfiguration("Default Web Site");

        ConfigurationSection handlersSection = webConfig.GetSection("system.webServer/handlers");

        ConfigurationElementCollection handlers = handlersSection.GetCollection();


        ConfigurationElement aspHandler = handlers.CreateElement();

        aspHandler["name"] = "ASPClassic";

        aspHandler["path"] = "*.asp";

        aspHandler["verb"] = "GET,HEAD,POST";

        aspHandler["modules"] = "IsapiModule";

        aspHandler["scriptProcessor"] = @"%windir%\system32\inetsrv\asp.dll";

        aspHandler["resourceType"] = "File";

        handlers.Add(aspHandler);

    }


    // 檢查ASP.NET是否已配置

    static bool IsASPNETConfigured(ServerManager serverManager)

    {

        try

        {

            var siteConfig = serverManager.GetWebConfiguration("Default Web Site");

            var handlers = siteConfig.GetSection("system.webServer/handlers").GetCollection();

            

            foreach (var handler in handlers)

            {

                if (handler["path"].ToString() == "*.aspx" && 

                    handler["type"] != null)

                {

                    return true;

                }

            }

            return false;

        }

        catch

        {

            return false;

        }

    }


    // 配置ASP.NET支持

    static void ConfigureASPNET(ServerManager serverManager)

    {

        // 設(shè)置應(yīng)用程序池使用.NET 4.x

        bool poolExists = false;

        foreach (var pool in serverManager.ApplicationPools)

        {

            if (pool.Name == "DefaultAppPool")

            {

                pool.ManagedRuntimeVersion = "v4.0";

                pool.ManagedPipelineMode = ManagedPipelineMode.Integrated;

                pool.AutoStart = true;

                pool.Enable32BitAppOnWin64 = true; // 根據(jù)需求設(shè)置

                poolExists = true;

                break;

            }

        }


        if (!poolExists)

        {

            ApplicationPool newPool = serverManager.ApplicationPools.Add("DefaultAppPool");

            newPool.ManagedRuntimeVersion = "v4.0";

            newPool.ManagedPipelineMode = ManagedPipelineMode.Integrated;

        }


        // 確保默認網(wǎng)站使用正確的應(yīng)用程序池

        Site defaultSite = serverManager.Sites["Default Web Site"];

        if (defaultSite != null)

        {

            defaultSite.ApplicationDefaults.ApplicationPoolName = "DefaultAppPool";

            

            // 添加ASP.NET處理程序映射

            Configuration siteConfig = serverManager.GetWebConfiguration("Default Web Site");

            ConfigurationSection handlersSection = siteConfig.GetSection("system.webServer/handlers");

            ConfigurationElementCollection handlers = handlersSection.GetCollection();

            

            // 檢查是否已存在

            bool handlerExists = false;

            foreach (var handler in handlers)

            {

                if (handler["path"].ToString() == "*.aspx")

                {

                    handlerExists = true;

                    break;

                }

            }

            

            if (!handlerExists)

            {

                ConfigurationElement aspxHandler = handlers.CreateElement();

                aspxHandler["name"] = "ASP.NET";

                aspxHandler["path"] = "*.aspx";

                aspxHandler["verb"] = "*";

                aspxHandler["type"] = "System.Web.UI.PageHandlerFactory";

                aspxHandler["resourceType"] = "Unspecified";

                aspxHandler["requireAccess"] = "Script";

                handlers.Add(aspxHandler);

            }

        }

    }


    // 重啟計算機

    static void RestartComputer()

    {

        ProcessStartInfo startInfo = new ProcessStartInfo

        {

            FileName = "shutdown.exe",

            Arguments = "/r /t 0",

            Verb = "runas", // 管理員權(quán)限

            UseShellExecute = true

        };


        try

        {

            Process.Start(startInfo);

            Environment.Exit(0);

        }

        catch (Exception ex)

        {

            throw new Exception($"重啟失敗: {ex.Message}\n請手動重啟計算機以完成安裝");

        }

    }

}

代碼功能說明:

1、智能檢測機制

  • IsASPInstalled():檢查經(jīng)典ASP功能是否安裝

  • IsASPNETInstalled():檢查ASP.NET功能是否安裝

  • IsASPConfigured():檢查ASP是否配置正確

  • IsASPNETConfigured():檢查ASP.NET是否配置正確

2、安裝功能

  • 使用DISM安裝缺失的IIS功能:

  • IIS-ASP:經(jīng)典ASP支持

  • IIS-ASPNET45:ASP.NET 4.x支持

  • 自動處理安裝結(jié)果和錯誤

?3、配置IIS

 經(jīng)典ASP配置

  • 啟用asp.dll ISAPI擴展

  • 配置父路徑、緩沖和錯誤顯示

  • 添加*.asp處理程序映射

 ASP.NET配置

  • 設(shè)置應(yīng)用程序池使用.NET 4.0

  • 配置集成管道模式

  • 添加*.aspx處理程序映射

  • 確保默認網(wǎng)站使用正確的應(yīng)用池

4、重啟管理

  • 需要重啟時提示用戶

  • 自動以管理員權(quán)限執(zhí)行重啟

使用說明:

1、項目配置(.csproj文件):

<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net6.0-windows</TargetFramework> <Platforms>x64</Platforms> <RuntimeIdentifier>win10-x64</RuntimeIdentifier> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <ItemGroup> <PackageReference Include="Microsoft.Web.Administration" Version="11.1.0" /> </ItemGroup></Project>

2、運行要求

  • 必須以管理員身份運行

  • Windows 7/Server 2008 R2或更高版本

  • 需要互聯(lián)網(wǎng)連接以下載IIS組件(如果需要)

3、驗證方法

  • 創(chuàng)建測試文件 C:\inetpub\wwwroot\test.asp

<%@ Language=VBScript %>

<% Response.Write("ASP Works! Time: " & Now()) %>

  • 創(chuàng)建測試文件 C:\inetpub\wwwroot\test.aspx

<%@ Page Language="C#" %>

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)

    {

        lblMessage.Text = "ASP.NET Works! Time: " + DateTime.Now;

    }

</script>

<html>

<body>

    <asp:Label ID="lblMessage" runat="server" />

</body>

</html>

  • 訪問以下URL驗證:

  http://localhost/test.asp

  http://localhost/test.aspx

處理流程:

注意事項:

1、重啟要求

  • 安裝IIS組件后需要重啟才能繼續(xù)配置

  • 代碼會自動提示重啟,重啟后需再次運行程序完成配置

2、自定義站點

  • 默認配置"Default Web Site"

  • 如需自定義站點,修改代碼中的站點名稱

3、32位支持

  • 默認啟用32位應(yīng)用支持(Enable32BitAppOnWin64 = true

  • 對于純64位環(huán)境,可設(shè)置為false

4、錯誤處理

  • 代碼包含詳細錯誤處理

  • 安裝失敗時會顯示具體原因

  • 權(quán)限不足時會提示使用管理員身份運行

此代碼提供了完整的端到端解決方案,從檢測、安裝到配置,全面支持ASP和ASP.NET頁面,確保您的網(wǎng)站能同時處理.asp和.aspx文件。


該文章在 2025/6/11 9:21:47 編輯過
關(guān)鍵字查詢
相關(guān)文章
正在查詢...
點晴ERP是一款針對中小制造業(yè)的專業(yè)生產(chǎn)管理軟件系統(tǒng),系統(tǒng)成熟度和易用性得到了國內(nèi)大量中小企業(yè)的青睞。
點晴PMS碼頭管理系統(tǒng)主要針對港口碼頭集裝箱與散貨日常運作、調(diào)度、堆場、車隊、財務(wù)費用、相關(guān)報表等業(yè)務(wù)管理,結(jié)合碼頭的業(yè)務(wù)特點,圍繞調(diào)度、堆場作業(yè)而開發(fā)的。集技術(shù)的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業(yè)的高效ERP管理信息系統(tǒng)。
點晴WMS倉儲管理系統(tǒng)提供了貨物產(chǎn)品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質(zhì)期管理,貨位管理,庫位管理,生產(chǎn)管理,WMS管理系統(tǒng),標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務(wù)都免費,不限功能、不限時間、不限用戶的免費OA協(xié)同辦公管理系統(tǒng)。
Copyright 2010-2025 ClickSun All Rights Reserved