【问题标题】:404 Page works on localhost but not on production (Azure Web App)404 页面适用于 localhost 但不适用于生产(Azure Web App)
【发布时间】:2018-08-22 18:36:46
【问题描述】:

我的本​​地主机上有一个 404 页面,它工作得很好。但是,当它被推送到 Azure Web App 时,它不会。

我最初是通过发布工具推送它,现在我使用从 Github 的一个分支推送的内置功能。

我有以下 web.config:

<system.web>
    <customErrors mode="On" defaultRedirect="~/Error/Index">
      <error redirect="~/Error/NotFound" statusCode="404" />
      <error redirect="~/Error/Index" statusCode="500" />

    </customErrors>
    <!-- More stuff :-) -->
  </system.web>
  <system.webServer>

这是我的错误控制器:

 public class ErrorController : Controller
    {
        public ViewResult Index()
        {
            return View("Error");
        }
        public ViewResult NotFound()
        {
            Response.StatusCode = 404; 
            return View("NotFound");
        }
    }

我的生产 web.config 转换文件根本没有更改(查看 cmets 中的内容):

<?xml version="1.0"?>

<!-- For more information on using Web.config transformation visit http://go.microsoft.com/fwlink/?LinkId=301874 -->

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <!--
    In the example below, the "SetAttributes" transform will change the value of
    "connectionString" to use "ReleaseSQLServer" only when the "Match" locator
    finds an attribute "name" that has a value of "MyDB".

    <connectionStrings>
      <add name="MyDB"
        connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"
        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    </connectionStrings>
  -->
  <system.web>
    <compilation xdt:Transform="RemoveAttributes(debug)" />
    <!--
      In the example below, the "Replace" transform will replace the entire
      <customErrors> section of your Web.config file.
      Note that because there is only one customErrors section under the
      <system.web> node, there is no need to use the "xdt:Locator" attribute.

      <customErrors defaultRedirect="GenericError.htm"
        mode="RemoteOnly" xdt:Transform="Replace">
        <error statusCode="500" redirect="InternalError.htm"/>
      </customErrors>
    -->
  </system.web>
</configuration>

然而,虽然我的 404 页面在 localhost 中运行良好,但在生产中我得到:

The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

而不是漂亮的 404 页面。

有什么想法吗?

【问题讨论】:

  • 拉斯,我回答错了,所以我删除了

标签: asp.net asp.net-mvc azure asp.net-mvc-5 azure-web-app-service


【解决方案1】:

我的本​​地主机上有一个 404 页面,它工作得很好。但是,当它被推送到 Azure Web App 时,它不会。

我使用了你的代码并重现了你的结果。

我在 web.config 中将&lt;httpErrors existingResponse="PassThrough" /&gt; 添加到&lt;system.webServer&gt; &lt;/system.webServer&gt;,然后它就可以正常工作了。

<system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules>
      <remove name="ApplicationInsightsWebTracking" />
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
    </modules>
    <httpErrors existingResponse="PassThrough" />
</system.webServer>

您可以参考这个article了解更多详情。

【讨论】:

    猜你喜欢
    • 2013-07-13
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 2022-08-16
    • 2020-05-13
    • 2016-11-08
    相关资源
    最近更新 更多