I created a very minimalist web api using Visual Studio Code for deploying on Azure for testing purpose. The api controller simply return OK on the url http://localhost:5000/api/test

It works pretty well on my local computer (using the Debugger for Chrome extension). When publishing on Azure (using the Azure App Service extension) it succeed publishing but when browsing to https://testapi30.azurewebsites.net/api/test I got You do not have permission to view this directory or page.

Then I activated diagnostic logs for this Azure app and see this:

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.

I didn't understand why I only have this problem on Azure and not locally. Why is necessary to have a default document when I try to hit specifically https://testapi30.azurewebsites.net/api/test

My question is what's wrong with my code showed below ? What do I have to do to have my web api working on 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");
        }

    }
}

UPDATE

Below are the trace logs:

As I see on the logs, the access to the path home\site\wwwroot\ is problematic. I don't have any wwwroot in my project because it is a web api. Maybe my web.config is not complete. Any suggestions ?

enter image description here

enter image description here

Below is a screenshot of my project tree.

enter image description here

SOLUTION

Finally got it working. Simply execute in terminal dotnet publish -c Release -o ./publish then right click on the publish folder and Deploy to web app

I don't know why I cannot simple deploy it using the Deploy button in the extension.

enter image description here

1 Answers

0
pappu On

Finally got it working. Simply execute in terminal dotnet publish -c Release -o ./publish then right click on the publish folder and Deploy to web app.

I don't know why I cannot simply deploy it using the Deploy button in the extension.