asp.net 自定义 404 页面 - 不工作

asp.net custom 404 page - not working

我是一名 php 开发人员,但必须对 asp 站点进行一些更改。我正在尝试将 404 错误重定向到自定义 404 页面。下面是 web.config 页面的代码:

<?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <system.webServer>
            <security>
                <requestFiltering>
                    <filteringRules>
                        <!-- name the rule -->
                        <filteringRule name="user agent deny" scanUrl="false" scanQueryString="false">
                            <scanHeaders>
                            <!-- apply rule to user-agent header -->
                                <add requestHeader="user-agent" />
                            </scanHeaders>
                            <!-- apply rule to all files -->
                            <appliesTo />
                            <denyStrings>
                                <clear />
            <!-- block the following bots -->
    <add string="Mozilla/5.0 (compatible; YandexBot/3.0; +http://yandex.com/bots) " />
    <add string="Mozilla/5.0 (compatible; YandexImages/3.0; +http://yandex.com/bots) " />
    <add string="Mozilla/5.0 (compatible; YandexBot/3.0; MirrorDetector; +http://yandex.com/bots) " />
    <add string="Mozilla/5.0 (compatible; YandexMedia/3.0; +http://yandex.com/bots) " />
    <add string="Mozilla/5.0 (compatible; YandexBlogs/0.99; robot; +http://yandex.com/bots) " />
    <add string="Mozilla/5.0 (compatible; YandexVideo/3.0; +http://yandex.com/bots) " />
    <add string="Mozilla/5.0 (compatible; YandexZakladki/3.0; +http://yandex.com/bots) " />
    <add string="Mozilla/5.0 (compatible; YandexAntivirus/2.0; +http://yandex.com/bots) " />
    <add string="Mozilla/5.0 (compatible; YandexFavicons/1.0; +http://yandex.com/bots) " />
    <add string="Mozilla/5.0 (compatible; YandexDirect/3.0; +http://yandex.com/bots) " />
    <add string="Mozilla/5.0 (compatible; YandexCatalog/3.0; +http://yandex.com/bots) " />
    <add string="Mozilla/5.0 (compatible; YandexImageResizer/2.0; +http://yandex.com/bots) " />
    <add string="Yandex/1.01.001 (compatible; Win16; I) " />
    <add string="Yandex/1.01.001 (compatible; Win16; H) " />
    <add string="Mozilla/5.0 (compatible; YandexWebmaster/2.0; +http://yandex.com/bots) " />
    <add string="Yandex/1.01.001 (compatible; Win16; P) " />
    <add string="Yandex/1.01.001 (compatible; Win16; m) " />
    <add string="YandexSomething/1.0 " />
    <add string="Baiduspider+(+http://www.baidu.com/search/spider.htm) " />
    <add string="BaiduImagespider+(+http://www.baidu.jp/search/s308.html) " />
    <add string="BaiDuSpider " />
    <add string="Baiduspider+(+http://help.baidu.jp/system/05.html) " />
    <add string="Baiduspider+(+http://www.baidu.com/search/spider_jp.html) " />
    <add string="Baiduspider+(+http://www.baidu.jp/spider/) " />
                            </denyStrings>
                        </filteringRule>
                    </filteringRules>
                </requestFiltering>
           </security>

           <staticContent>
               <clientCache cacheControlMaxAge="365.00:00:00" cacheControlMode="UseMaxAge" />
           </staticContent>

           <defaultDocument>
               <files>
                   <clear />
            <add value="index.asp" />
            <add value="Default.asp" />
            <add value="Index.html" />
            <add value="Index.htm" />
            <add value="Index.cfm" />
            <add value="Index.shtml" />
            <add value="Index.shtm" />
            <add value="Index.stm" />
            <add value="Index.php" />
            <add value="Index.php3" />
            <add value="Index.aspx" />
            <add value="Default.htm" />
            <add value="Default.aspx" />
               </files>
           </defaultDocument>

           <rewrite>
               <rules>
                   <rule name="Redirect to WWW" stopProcessing="true">
                       <match url=".*" />
                       <conditions>
                           <add input="{HTTP_HOST}" pattern="^thetoolstore.ca$" />
                       </conditions>
                       <action type="Redirect" url="http://www.thetoolstore.ca/{R:0}" redirectType="Permanent" />
                   </rule>

                   <rule name="Redirect to root" patternSyntax="Wildcard" stopProcessing="true">
                   <match url="index.asp" />
                       <action type="Redirect" url="/" appendQueryString="false" redirectType="Permanent" />
                   </rule>
                   <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>
    <httpErrors errorMode="Detailed" />


</system.webServer>
<system.net>
    <mailSettings>
        <smtp from="info@thetoolstore.ca">
            <network host="localhost" />
        </smtp>
    </mailSettings>
</system.net>

我尝试添加如下代码:

<httpErrors errorMode="Custom" existingResponse="Replace">
    <remove statusCode="404" subStatusCode="-1" />
    <error statusCode="404" path="404.asp" responseMode="ExecuteURL"/>
</httpErrors>

但这只是给出了 500 错误。实际上,我尝试的任何操作都出现了 500 错误。

此外,我很确定这不是 MVC 网站,如果有帮助的话。

谢谢!

它给你一个 500 错误,因为 web.config 文件本身格式不正确。它缺少结束标记 </configuration>.

并且还有一个现有的 httpErrors 节点 <httpErrors errorMode="Detailed" />。必须将其替换为您的代码(有效)。不能重复。

最后请使用 http://www.freeformatter.com/xml-formatter.html 之类的东西来正确格式化 web.config 文件。乱七八糟的。