c# - Azure Web App - 多个应用程序托管 - 访问 Web API 但显示来自 Web App 的 web.config 的错误

标签 c# asp.net azure

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

这就是我的文件夹的样子:

enter image description here

这是我得到的错误:

Click here to see my error

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

更新:

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

最佳答案

我已经解决了我自己的问题。 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

关于c# - Azure Web App - 多个应用程序托管 - 访问 Web API 但显示来自 Web App 的 web.config 的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51126429/

相关文章:

Azure - 网站在 RDP 文件系统中存储在哪里?

azure - 从azure应用程序服务访问虚拟机上的服务

azure - azure 表存储中行数的上限...?

c# - 使用 Properties.Settings 保存应用程序设置不起作用?

c# - LINQ 跟踪变化

c# - 以编程方式将 StackPanel 添加到 ComboBoxItem

c# - WPF C# 4.5 ListBox 更新非常慢

asp.net - .net 能否知道客户端是否支持 HTTP 上的 SNI?

javascript - 使用谷歌分析计算网站上的电影播放次数

asp.net - 从 asp.net web 应用程序中删除站点移动母版页