【问题标题】:Enabling URL Rewriting with ASP.NET using web.config使用 web.config 启用 ASP.NET 的 URL 重写
【发布时间】:2013-01-08 03:07:54
【问题描述】:

我是ASP.NET 的新手,实际上我对此一无所知,但不要绝望,我不是在寻找教程...我的一个同事想要在我们的网络服务器上运行ASP.NET 应用程序。我们的网络服务器运行的是 Windows 2008 R2,带有 Plesk 11 和 IIS 7.5。

到目前为止,我在尝试启用 ASP.NET 时遇到了各种各样的问题,但经过一番折腾,我设法让应用程序正常工作(在一定程度上)。

但是,为了让应用程序正常工作,我不得不从我的 web.config 文件中删除以下行:

<configSections>
    <sectionGroup name="system.webServer">
        <sectionGroup name="rewrite">
            <section name="rewriteMaps" overrideModeDefault="Allow" />
            <section name="rules" overrideModeDefault="Allow" />
        </sectionGroup>
    </sectionGroup>
</configSections>

这是因为我收到了错误:

解析器错误消息:部分或组名称“system.webServer”是 已经定义了。对此的更新可能只发生在配置中 定义的级别。

我的文件夹结构是这样的:

website/
    TestApplication/
        web.config
    index.php
    web.config

我已经为文件夹“TestApplication”创建了一个应用程序(并不是说我真的知道这意味着什么),但这并没有任何区别。

我的印象是,如果您创建一个应用程序,目录中的web.config不会继承任何父配置!这似乎不会就是这种情况,因为我仍然收到错误。

我的问题是,对于上述情况,我该怎么办?如何阻止应用程序继承父目录配置,或者修复当前的web.config 文件以使用ASP.NET

需要注意的是,我在 IIS 管理器和 Plesk 中都进行了更改,因为我无法在 Plesk 控制面板中找到特定的内容,例如:为网站中的目录创建应用程序。我认为这不是问题的原因,但是,值得一提。

更新

这是我的完整 web.config 文件(删除了我的所有规则之一,因为它们对文件没有影响):

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

<configSections>
    <sectionGroup name="system.webServer">
        <sectionGroup name="rewrite">
            <section name="rewriteMaps" overrideModeDefault="Allow" />
            <section name="rules" overrideModeDefault="Allow" />
        </sectionGroup>
    </sectionGroup>
</configSections>

    <system.webServer>
        <httpErrors errorMode="Detailed" />
        <asp scriptErrorSentToBrowser="true"/>
        <rewrite>
            <rules>
                <clear />
                <rule name="Browser Error Rewrite" stopProcessing="true">
                    <match url="^errors/browser$" />
                    <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
                    <action type="Rewrite" url="_errors/browser.php?error=browser" />
                </rule>
            </rules>
        </rewrite>
        <defaultDocument>
            <files>
                <clear />
                <add value="index.php" />
            </files>
        </defaultDocument>
        <httpErrors>
            <clear />
            <error statusCode="400" prefixLanguageFilePath="" path="/_errors/error.php?id=400" responseMode="ExecuteURL" />
            <error statusCode="502" prefixLanguageFilePath="" path="/_errors/error.php?id=502" responseMode="ExecuteURL" />
            <error statusCode="407" prefixLanguageFilePath="" path="/_errors/error.php?id=407" responseMode="ExecuteURL" />
            <error statusCode="414" prefixLanguageFilePath="" path="/_errors/error.php?id=414" responseMode="ExecuteURL" />
            <error statusCode="415" prefixLanguageFilePath="" path="/_errors/error.php?id=415" responseMode="ExecuteURL" />
            <error statusCode="501" prefixLanguageFilePath="" path="/_errors/error.php?id=501" responseMode="ExecuteURL" />
            <error statusCode="500" prefixLanguageFilePath="" path="/_errors/error.php?id=500" responseMode="ExecuteURL" />
            <error statusCode="401" prefixLanguageFilePath="" path="/_errors/error.php?id=401" responseMode="ExecuteURL" />
            <error statusCode="403" prefixLanguageFilePath="" path="/_errors/error.php?id=403" responseMode="ExecuteURL" />
            <error statusCode="404" prefixLanguageFilePath="" path="/_errors/error.php?id=404" responseMode="ExecuteURL" />
            <error statusCode="405" prefixLanguageFilePath="" path="/_errors/error.php?id=405" responseMode="ExecuteURL" />
            <error statusCode="412" prefixLanguageFilePath="" path="/_errors/error.php?id=412" responseMode="ExecuteURL" />
            <error statusCode="406" prefixLanguageFilePath="" path="/_errors/error.php?id=406" responseMode="ExecuteURL" />
        </httpErrors>
        <handlers>
            <clear />
            <add name="PageHandlerFactory-Integrated" path="*.aspx" verb="GET,HEAD,POST,DEBUG" type="System.Web.UI.PageHandlerFactory" preCondition="integratedMode" />
            <add name="php-5.3.10" path="*.php" verb="GET,HEAD,POST" modules="FastCgiModule" scriptProcessor="C:\Program Files (x86)\Parallels\Plesk\Additional\PleskPHP53\php-cgi.exe" resourceType="Either" requireAccess="Script" />
            <add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" />
        </handlers>
    </system.webServer>
</configuration>

【问题讨论】:

  • 你尝试过 IIS url 重写模块吗? URL rewrite module
  • @RaviSilva 这就是创建这些configSections 块的原因,但无论如何感谢:-)。我已经整理好了,稍后会发布答案
  • @Ben Carey - 你是怎么解决的?
  • IIS URL 重写模块不会触及您的 web.config 并生成 configSections。如果是这样,那应该被认为是一个错误。您可以安全地将其从 web.config 文件中删除。

标签: asp.net iis web web-config plesk


【解决方案1】:

您的 web.config 中似乎有两次(这就是“解析器错误消息:部分或组名称 'system.webServer' 已定义”错误所暗示的)。

在应用程序的 web.config 中搜索并剪切其中的代码并将其粘贴到另一个(确保代码不会重复)删除您已从中剪切代码的那个

【讨论】:

  • 是的,这从错误消息中很清楚,但是,我无法删除其中一个,因为我需要它才能使 URL 重写工作!
  • 你可以在删除敏感信息后明显地发布你的 web.config 代码吗?
  • 没问题,给我两分钟 :-)
  • 你指的配置文件是主配置文件,不是应该编辑的。更不用说它会将该配置应用于我所有的网站!我已经发布了我的 web.config 文件
【解决方案2】:

如果您使用的是较新版本的 IIS 或 Windows Azure,则不需要&lt;ConfigSection&gt;。它内置在新的 IIS 中。直接在 web.config 中的 &lt;system.webServer&gt; 下使用 &lt;rewrite&gt; 标签即可。

【讨论】:

  • 不正确。仅当您使用 Azure 网站时,才会默认安装该模块。 Windows Server 或桌面尚未附带 IIS URL Rewrite。
猜你喜欢
  • 2014-08-12
  • 2012-12-06
  • 2015-06-27
  • 2010-09-26
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多