当前位置: 首页 > ds >正文

45、简述web.config⽂件中的重要节点

web.config 是 ASP.NET 应用程序中用于存储配置信息的 XML 文件,它允许开发者在不重新编译代码的情况下调整应用程序的行为。以下是 web.config 文件中一些重要的节点及其作用:

1. configuration

  • 作用:web.config 文件的根节点,所有其他配置节点都包含在其中。

2. appSettings

  • 作用:用于存储自定义的应用程序设置。这些设置可以在代码中通过 ConfigurationManager.AppSettings 访问。
  • 示例:
<appSettings><add key="Setting1" value="Value1" /><add key="Setting2" value="Value2" />
</appSettings>

3. connectionStrings

  • 作用:用于配置数据库连接字符串。这些连接字符串可以在代码中通过
    ConfigurationManager.ConnectionStrings 访问。
  • 示例:
<connectionStrings><add name="MyDatabase" connectionString="Data Source=.;Initial Catalog=MyDB;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>

4.4. <system.web>

  • 作用:包含与 ASP.NET 运行时行为相关的配置设置。

  • 子节点:

    • authentication:配置应用程序的身份验证模式(如 Windows、Forms、 Passport、None)。
    • authorization:控制对应用程序资源的访问权限。
    • compilation:配置编译选项,如调试模式、目标框架版本等。
    • httpRuntime:配置 HTTP 运行时设置,如请求超时、最大请求长度等。
    • sessionState:配置会话状态存储方式(如 InProc、StateServer、SQLServer)。
    • customErrors:配置自定义错误页面,用于在发生错误时显示友好的消息。
  • 示例:

<system.web><authentication mode="Forms"><forms loginUrl="Login.aspx" timeout="2880" /></authentication><authorization><deny users="?" /></authorization><compilation debug="true" targetFramework="4.8" /><httpRuntime executionTimeout="90" maxRequestLength="4096" /><sessionState mode="InProc" timeout="20" /><customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm"><error statusCode="403" redirect="NoAccess.htm" /><error statusCode="404" redirect="FileNotFound.htm" /></customErrors>
</system.web>

5. system.webServer

  • 作用:包含与 IIS 7.0 及更高版本相关的配置设置。

  • 子节点:

    • modules:配置 HTTP 模块。
    • handlers:配置 HTTP 处理程序。
    • rewrite:配置 URL 重写规则。
  • 示例:

<system.webServer><modules runAllManagedModulesForAllRequests="true" /><handlers><add name="MyHandler" path="*.myext" verb="*" type="MyNamespace.MyHandler" resourceType="Unspecified" preCondition="integratedMode" /></handlers><rewrite><rules><rule name="Redirect to HTTPS" stopProcessing="true"><match url="(.*)" /><conditions><add input="{HTTPS}" pattern="^OFF$" /></conditions><action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" /></rule></rules></rewrite>
</system.webServer>

6. location

  • 作用:用于对特定目录或文件应用配置设置。
  • 示例:
<location path="Admin"><system.web><authorization><deny users="*" /><allow roles="Administrators" /></authorization></system.web>
</location>

7. runtime

  • 作用:配置 .NET 运行时行为,如程序集绑定、代码访问安全等。
  • 示例:
<runtime><assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"><dependentAssembly><assemblyIdentity name="MyAssembly" publicKeyToken="..." culture="neutral" /><bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" /></dependentAssembly></assemblyBinding>
</runtime>

8. system.codedom

  • 作用:配置代码编译器的设置,如 C# 或 VB.NET 的编译器选项。
  • 示例:
<system.codedom><compilers><compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" /></compilers>
</system.codedom>

总结

  • IIS 6 及之前:主要使用 <system.web>。
  • IIS 7+:优先使用 <system.webServer>,可与 <system.web> 共存(后者可能被覆盖)。
  • 现代应用:ASP.NET Core 使用 appsettings.json,但传统 ASP.NET 仍依赖 web.config。

通过合理配置这些节点,可以优化性能、增强安全性并实现灵活的请求处理。

在这里插入图片描述

http://www.xdnf.cn/news/7057.html

相关文章:

  • Leaflet使用SVG创建动态Legend
  • 文件读取漏洞路径与防御总结
  • AI日报 - 2024年5月17日
  • PyTorch实现三元组损失Triplet Loss
  • 风控域——风控决策引擎系统设计
  • 考研数学微分学(第三,四,五,六,七讲)
  • 【前端基础】HTML元素隐藏的四个方法(display设置为none、visibikity设置为hidden、rgba设置颜色、opacity设置透明度)
  • 软件设计师教程—— 第二章 程序设计语言基础知识(上)
  • Spatial Transformer Layer
  • Vue3学习(组合式API——ref模版引用与defineExpose编译宏函数)
  • 信贷域——互联网金融业务
  • 低空经济发展现状与前景
  • 聚集索引 vs. 非聚集索引
  • 恒大歌舞团全集
  • Android 14 解决打开app出现不兼容弹窗的问题
  • 参考工具/网站
  • scss additionalData Can‘t find stylesheet to import
  • 强化学习入门:马尔科夫奖励过程二
  • 什么是API接口?API接口的核心价值
  • 网关GateWay——连接不同网络的关键设备
  • STM32IIC实战-OLED模板
  • TC3xx学习笔记-UCB BMHD使用详解(二)
  • 使用NVM管理node版本
  • GO语言学习(二)
  • CSS 浮动与定位以及定位中z-index的堆叠问题
  • 设计练习 - Movie Review Aggregator System
  • 探秘Transformer系列之(33)--- DeepSeek MTP
  • 【爬虫】DrissionPage-6
  • MapReduce 原理深度剖析:从任务执行到参数配置
  • AI编码代理的崛起 - AlphaEvolve与Codex的对比分析引言