c# - 无法访问 Azure 上发布的 Web api

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

我使用 Visual Studio Code 创建了一个非常简约的 Web API,以便在 Azure 上部署以进行测试。 api Controller 只需在 url http://localhost:5000/api/test

上返回 OK

它在我的本地计算机上运行得很好(使用 Chrome 调试器扩展)。在 Azure 上发布(使用 Azure 应用服务扩展)时,它会成功发布,但在浏览到 https://testapi30.azurewebsites.net/api/test 时,它会成功发布。我收到您无权查看此目录或页面。

然后我激活了此 Azure 应用程序的诊断日志并看到了以下内容:

The Web server is configured to not list the contents of this directory. Most likely causes: A default document is not configured for the requested URL, and directory browsing is not enabled on the server. Things you can try: If you do not want to enable directory browsing, ensure that a default document is configured and that the file exists. Enable directory browsing using IIS Manager. More Information: This error occurs when a document is not specified in the URL, no default document is specified for the Web site or application, and directory listing is not enabled for the Web site or application. This setting may be disabled on purpose to secure the contents of the server.

我不明白为什么我只在 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 不完整。有什么建议 ?

enter image description here

enter image description here

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

enter image description here

解决方案

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

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

enter image description here

最佳答案

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

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

关于c# - 无法访问 Azure 上发布的 Web api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55918840/

相关文章:

c# - "Syntax error at position 20 in Timestamp"Azure表存储查询问题

azure - 对于 '10/15/2018' 问题之后创建的此类应用程序,不支持使用/common 端点

visual-studio-code - VSCode : How to programmatically set a handler on editor's language change event in my extension?

C#计算器指数函数。如何?

c# - 如何检查日期时间是否等于 20 :00 *local* time

c# - 使 WinForm 更改背景颜色但仍可用

Azure - 从免费服务转向基本移动服务按需付费

bash - 无法在 Azure Powershell 中键入密码

visual-studio-code - 在带有 makefile 的 VSCODE 中使用英特尔 Fortran 编译器 - `make: ifort: Command not found`

visual-studio-code - 如何在 VS Code 的事件栏中设置事件元素的样式?