404页面可在localhost上运行,但不能在生产环境上运行(Azure Web App) [英] 404 Page works on localhost but not on production (Azure Web App)

查看:107
本文介绍了404页面可在localhost上运行,但不能在生产环境上运行(Azure Web App)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地主机上有一个404页面,可以正常工作.但是,当将其推送到Azure Web App时,它不会.

我最初是通过发布工具来推送它的,现在我使用从Github中的分支推送的内置功能.<​​/p>

我有以下 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转换文件完全没有更改(请参阅注释中的内容):

<?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页面.

有什么主意吗?

解决方案

我在本地主机上有一个404页面,可以正常工作.但是,当将其推送到Azure Web App时,不会.

我使用了您的代码并复制了结果.

我在web.config中将<httpErrors existingResponse="PassThrough" />添加到<system.webServer> </system.webServer>中,然后它可以正常工作.

<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>

您可以参考以下文章有关更多详细信息.

I have a 404 page on my localhost which works just fine. However, when it's pushed to Azure Web App, it does not.

I pushed it originally through the Publish tool, and right now I use the built in feature that pushes from a branch in Github.

I have the following 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>

This is my error controller:

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

My production web.config transformation file has not been changed at all (see how stuff is in comments):

<?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>

However, while my 404 page works just fine in localhost, in production I get:

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

Instead of a nice looking 404 page.

Any idea?

解决方案

I have a 404 page on my localhost which works just fine. However, when it's pushed to Azure Web App, it does not.

I used your code and reproduced your results.

I add <httpErrors existingResponse="PassThrough" /> to <system.webServer> </system.webServer> in the web.config ,then it works fine.

<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>

You can refer to this article for more details.

这篇关于404页面可在localhost上运行,但不能在生产环境上运行(Azure Web App)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆