简体   繁体   English

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

[英]Azure Web App - Multiple apps hosting - Access Web API but it shows error from web.config of Web App

I am not sure if this has been asked before. 我不确定以前是否曾经问过这个问题。 I am trying to deploy my two web applications to an azure web app, an asp.net web form application and an asp.net web api. 我正在尝试将我的两个Web应用程序部署到azure Web应用程序,asp.net Web表单应用程序和asp.net web api。 I do know that I can create two different directories and then put my two applications to those two directories. 我知道我可以创建两个不同的目录,然后将我的两个应用程序放到这两个目录中。 However when I do that and configure like the first image below, my web app works well but my web API get a problem, an error from the web.config file of the web application is shown. 但是,当我这样做并配置如下面的第一张图片时,我的网络应用程序运行良好,但我的Web API出现问题,显示Web应用程序的web.config文件中的错误。 I've tried to disable my web application then the web API worked well after that. 我试图禁用我的Web应用程序,然后Web API在此之后运行良好。

This is what my folders look like: 这是我的文件夹的样子:

在此输入图像描述

And this is the error that I get: 这是我得到的错误:

点击这里查看我的错误

There is a similar thing that has been posted before here Azure multiple sites in virtual directories but in my case, our applications are not built on ASP.NET CORE 在此之前已经发布了类似的事情,在Azure虚拟目录中有多个站点,但在我的情况下,我们的应用程序不是基于ASP.NET CORE构建的

Update: 更新:

  • I have also tried to add the "System.Web.Optimization" dll to the web api project just to have a try (it actually is not needed) and after that I see another error. 我还尝试将“System.Web.Optimization”dll添加到web api项目中只是为了尝试(实际上不需要),然后我看到另一个错误。
  • Lines of code you see in the error image is just from web.config of the web application, that should not be a problem when I try to access the web api. 您在错误图像中看到的代码行仅来自Web应用程序的web.config,当我尝试访问Web API时,这应该不是问题。

I have solved my own problem. 我已经解决了自己的问题。 web.config files are inherited from root level as default, I tried to add <location path="." inheritInChildApplications='false'> web.config文件默认从根级别继承,我尝试添加<location path="." inheritInChildApplications='false'> <location path="." inheritInChildApplications='false'> to cover all sections (exclude ones that don't allow to do this) inside the web.config file of the web app (in this case, it's the root web.config file). <location path="." inheritInChildApplications='false'>覆盖Web应用程序的web.config文件中的所有部分(不包括那些不允许这样做的部分)(在这种情况下,它是根web.config文件)。

For instance 例如

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

Following this article https://www.hanselman.com/blog/ChangingASPNETWebconfigInheritanceWhenMixingVersionsOfChildApplications.aspx 以下文章https://www.hanselman.com/blog/ChangingASPNETWebconfigInheritanceWhenMixingVersionsOfChildApplications.aspx

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

Lines of code you see in the error image is just from web.config of the web application, that should not be a problem when I try to access the web api. 您在错误图像中看到的代码行仅来自Web应用程序的web.config,当我尝试访问Web API时,这应该不是问题。

As the second image you provided, when you visit some specific controller in api it also get error, so it means you could not get the web api correctly. 作为您提供的第二个图像,当您访问api中的某个特定控制器时,它也会出错,因此这意味着您无法正确获取Web api。

I try to test and fail not reproduce your error message. 我尝试测试并且无法重现您的错误消息。 You could refer to this thread to follow the detail steps to create a virtual directory. 您可以参考此线程以按照详细步骤创建虚拟目录。

According to your error message, you could try the several ways to troubleshoot: 根据您的错误消息,您可以尝试以下几种方法进行故障排除:

1.Uninstalled and reinstalled the nuget packages(update to the latest). 1.卸载并重新安装nuget软件包(更新到最新版本)。

2.Try to install Microsoft.AspNet.Web.Optimization via nuget(you do not work). 2.尝试通过nuget安装Microsoft.AspNet.Web.Optimization(你不工作)。

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

4.Set the web.config as below, and refer to this issue . 4.如下所示设置web.config,并参考此问题

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM