发送一个 Response.Status = "404 Not Found" 但让 IIS 保持在同一页面

Send a Response.Status = "404 Not Found" but let IIS remain on the same page

我有一个简单的问题:

是否可以从 .asp 页面发送 Response.Status = "404 Not Found",但继续呈现实际页面?

当我发送 Response.Status = "404 Not Found" 时,IIS 似乎移动到默认的 404 错误页面,而我想继续呈现我的页面。这是 CMS 的一个例子,出于某种原因有人可以键入不存在的永久链接并希望显示 "OPS not found content page" 而不是重定向到自定义或默认 404 asp 页面。

有特定的 web.config 设置吗?

我关注了这里的讨论: Configuring custom ASP 404 page with redirect for IIS7 website

并考虑使用 then 选项

<httpErrors existingResponse="PassThrough" />

但由于某种原因,它给了我错误 500。 IIS 版本为 8.5 asp 3.0 / .NET 3.5

我的实际 web.config 是:

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

<httpErrors errorMode="DetailedLocalOnly"/>  

<urlCompression doDynamicCompression="true" doStaticCompression="true" dynamicCompressionBeforeCache="true" />      
<caching enabled="true">
    <profiles>              
      <add extension=".gif" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="0.00:01:00" location="Any" />
      <add extension=".png" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="0.00:01:00" location="Any" />
      <add extension=".js" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="0.00:01:00" location="Any" />
      <add extension=".css" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="0.00:01:00" location="Any" />
      <add extension=".jpg" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="0.00:01:00" location="Any" />
      <add extension=".jpeg" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" duration="0.00:01:00" location="Any" />
    </profiles>
</caching>  

<staticContent>
    <remove fileExtension=".js" />
    <mimeMap fileExtension=".js" mimeType="text/javascript" />
    <remove fileExtension=".eot" />
    <remove fileExtension=".ttf" />
    <remove fileExtension=".otf"/>
    <remove fileExtension=".woff"/>
    <remove fileExtension=".woff2"/>
    <mimeMap fileExtension=".eot" mimeType="application/vnd.ms-fontobject" />
    <mimeMap fileExtension=".ttf" mimeType="font/ttf" />
    <mimeMap fileExtension=".otf" mimeType="font/otf" />
    <mimeMap fileExtension=".woff" mimeType="font/x-woff" />
    <mimeMap fileExtension=".woff2" mimeType="font/x-woff2" />      
    <clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" cacheControlMaxAge="30.00:00:00" />
</staticContent>        

<rewrite>
      <rules>
        <rule name="Rewrite to friendly URL">
              <match url="^site/([_0-9a-z-]+)" />
              <action type="Rewrite" url="/default.asp?pl={R:1}" />
        </rule>
      </rules>
</rewrite>  

提前感谢任何解决方案:)

编辑:

经过多次测试,我找到了解决方案:

<httpErrors errorMode="DetailedLocalOnly" existingResponse="PassThrough"/>  

如果我添加与 "DetailedLocalOnly" 分开的行,我得到的 500 错误现在使用带有两个参数的一行解决。

也许这只是您 web.config 中的错字。如果上面是你的配置文件,你忘记输入system.webServer标签级别。

我在 IIS 8.5 上对此进行了测试,它按预期工作。所以 ASP 生成 当页面返回 404 状态代码时显示内容。我真的只使用了以下配置:

<configuration>
  <system.webServer>
    <httpErrors existingResponse="PassThrough" />
  </system.webServer>
</configuration>

或者,但不推荐,它也适用于以下配置:

<configuration>
  <system.web>
    <customErrors mode="Off" />
  </system.web>
  <system.webServer>
    <httpErrors errorMode="Detailed" />
  </system.webServer>
</configuration>

有兴趣的,这里是 link 到 documentation