【问题标题】:Azure Web App - Multiple apps hosting - Access Web API but it shows error from web.config of Web AppAzure Web App - 多个应用程序托管 - 访问 Web API,但它显示来自 Web App 的 web.config 的错误
【发布时间】:2018-07-01 19:36:16
【问题描述】:

我不确定以前是否有人问过这个问题。我正在尝试将我的两个 Web 应用程序部署到一个 azure Web 应用程序、一个 asp.net Web 表单应用程序和一个 asp.net Web api。 我知道我可以创建两个不同的目录,然后将我的两个应用程序放到这两个目录中。但是,当我这样做并像下面的第一张图片进行配置时,我的 Web 应用程序运行良好,但我的 Web API 出现问题,显示来自 Web 应用程序的 web.config 文件的错误。我尝试禁用我的 Web 应用程序,然后 Web API 运行良好。

这是我的文件夹的样子:

这是我得到的错误:

之前在这里发布过类似的内容Azure multiple sites in virtual directories,但就我而言,我们的应用程序不是基于 ASP.NET CORE 构建的

更新:

  • 我还尝试将“System.Web.Optimization”dll 添加到网络 api项目只是为了试一试(实际上不需要)和之后 我看到另一个错误。
  • 您在错误图像中看到的代码行仅来自 web.config 的Web应用程序,当我尝试时这应该不是问题 访问网络 API。

【问题讨论】:

  • 它不起作用。

标签: c# asp.net azure


【解决方案1】:

我已经解决了我自己的问题。 web.config 文件默认从根级别继承,我尝试添加 <location path="." inheritInChildApplications='false'> 以覆盖 Web 应用程序的 web.config 文件中的所有部分(不包括不允许这样做的部分)(在这种情况下,它是根 web.config 文件)。

例如

<location path="." inheritInChildApplications='false'>
  <connectionStrings>
    <add name="ApplicationDbContext" connectionString="Data Source=.;Initial Catalog=xxx;Integrated Security=True" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="AngularAppName" value="angularApp" />
  </appSettings>
  <system.web>
    <customErrors mode="Off" />
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime targetFramework="4.6.1" />
    <pages>
    <namespaces>
            <add namespace="System.Web.Optimization" />
          </namespaces>
      <controls>
        <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
      </controls>
    </pages>
  </system.web>
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.8.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
    </compilers>
  </system.codedom>
  <entityFramework>

    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  </location>

关注本文https://www.hanselman.com/blog/ChangingASPNETWebconfigInheritanceWhenMixingVersionsOfChildApplications.aspx

【讨论】:

    【解决方案2】:

    您需要在 Azure Web 应用程序的应用程序设置页面下添加新的虚拟应用程序或目录

    【讨论】:

      【解决方案3】:

      您在错误图像中看到的代码行只是来自 web 应用程序的 web.config,当我尝试访问 web api 时应该没有问题。

      作为您提供的第二张图片,当您访问 api 中的某些特定控制器时,它也会出错,因此这意味着您无法正确获取 web api。

      我尝试测试并失败,但不会重现您的错误消息。您可以参考此thread 以按照详细步骤创建虚拟目录。

      根据您的报错信息,您可以尝试以下几种方式进行故障排除:

      1.卸载并重新安装nuget包(更新到最新)。

      2.尝试通过nuget安装Microsoft.AspNet.Web.Optimization(你不行)。

      3.为 System.Web.Optimiszati​​on 设置属性 CopyLocal=True。

      4.如下设置web.config,参考issue

      <configuration>
        <system.webServer>
          <handlers>
            <add name="httpplatformhandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" />
          </handlers>
        </system.webServer>
      </configuration>
      

      【讨论】:

      • 编辑 web.config 后,我看到此错误消息:“指定的 CGI 应用程序遇到错误,服务器终止进程。”
      • httpPlatformremoveforwardWindowsAuthToken="true/false" 属性中,引用这个thread
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-04
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 1970-01-01
      • 1970-01-01
      • 2018-08-14
      相关资源
      最近更新 更多