【问题标题】:asp.net custom 404 page - not workingasp.net 自定义 404 页面 - 不工作
【发布时间】:2016-08-29 18:54:45
【问题描述】:

我是一名 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 网站,如果有帮助的话。

谢谢!

【问题讨论】:

    标签: asp.net http-status-code-404


    【解决方案1】:

    它给你一个 500 错误,因为 web.config 文件本身格式错误。它缺少结束标签&lt;/configuration&gt;

    还有一个现有的 httpErrors 节点&lt;httpErrors errorMode="Detailed" /&gt;。这必须用您的代码(有效)替换。不能有重复。

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

    【讨论】:

    • 谢谢!我会试试的。
    猜你喜欢
    • 2011-12-23
    • 2014-01-02
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 2019-09-14
    • 2014-01-21
    • 1970-01-01
    相关资源
    最近更新 更多