【问题标题】:Unable to hit web api published on Azure无法访问在 Azure 上发布的 Web api
【发布时间】:2019-09-18 23:48:12
【问题描述】:

我使用 Visual Studio Code 创建了一个非常简约的 Web api,用于在 Azure 上进行部署以进行测试。 api 控制器只需在 url http://localhost:5000/api/test 上返回 OK

它在我的本地计算机上运行良好(使用 Debugger for Chrome 扩展程序)。在 Azure 上发布(使用 Azure 应用服务扩展)时,它成功发布,但是当浏览到 https://testapi30.azurewebsites.net/api/test 时,我得到了 You do not have permission to view this directory or page.

然后我为这个 Azure 应用程序激活了诊断日志并看到了这个:

Web 服务器配置为不列出此内容 目录。最可能的原因: 未配置默认文档 对于请求的 URL,并且目录浏览未在 服务器。您可以尝试的事情:如果您不想启用目录 浏览,确保配置了默认文档并且 文件已存在。使用 IIS 管理器启用目录浏览。更多的 信息:当文档未在 URL,没有为网站或应用程序指定默认文档, 网站或应用程序未启用目录列表。 可能会故意禁用此设置以保护 服务器。

我不明白为什么我只在 Azure 而不是本地遇到这个问题。当我尝试专门点击https://testapi30.azurewebsites.net/api/test时,为什么需要有一个默认文档

我的问题是下面显示的代码有什么问题?我必须做什么才能让我的 web api 在 Azure 上运行?

api.csproj

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore" Version="2.2.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc" Version="2.2.0" />
    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="2.2.0" />
  </ItemGroup>

</Project>

web.config

<configuration> 

    <system.web>
        <customErrors mode="Off" />
        <authentication mode="None" />
    </system.web>

    <system.webServer>
        <modules runAllManagedModulesForAllRequests="true" />
        <httpErrors errorMode="Detailed"></httpErrors>
    </system.webServer> 

</configuration>

Program.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace API
{
    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();
    }
}

Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Cors.Infrastructure;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace API
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc(); 
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.UseMvc();
        }
    }
}

TestController.cs

using System;
using Microsoft.AspNetCore.Mvc;

namespace API.Controllers
{
    [Route("api/[controller]")]
    public class TestController : Controller
    {
        [HttpGet]
        public IActionResult Get()
        {
            return Content("OK");
        }

    }
}

更新

以下是跟踪日志:

正如我在日志中看到的,对路径 home\site\wwwroot\ 的访问存在问题。我的项目中没有任何wwwroot,因为它是一个 Web api。也许我的 web.config 不完整。有什么建议 ?

下面是我的项目树的截图。

解决方案

终于搞定了。只需在终端dotnet publish -c Release -o ./publish 中执行,然后右键单击发布文件夹和Deploy to web app

我不知道为什么我不能使用扩展程序中的Deploy 按钮简单地部署它。

【问题讨论】:

  • 如果您使用的 URL 是 实际 URL,您会收到不同的错误:配置错误 It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level.
  • 您的 localhost 端口是 5000。您是否也在尝试访问 Azure 中的端口 5000?
  • @DavidMakogon 我正在尝试访问 https://testapi30.azurewebsites.net/api/test 和 https://testapi30.azurewebsites.net 是我的 web api 发布的地方。
  • @rickvdbosch 你是什么意思?我没有看到你在我的日志中提到的错误,但如果你解释一下,我可以尝试一下。
  • @Bronzato 如果我单击链接testapi30.azurewebsites.net/api/test,我会收到一条配置错误,说明Description: An error occurred during the processing of a configuration file required to service this request.,这表明您的web.config 文件的配置无效。

标签: c# azure visual-studio-code asp.net-core-webapi


【解决方案1】:

终于搞定了。只需在终端中执行 dotnet publish -c Release -o ./publish 然后右键单击发布文件夹并部署到 Web 应用程序。

我不知道为什么我不能简单地使用扩展中的部署按钮来部署它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多