ASP.NET Core 1.1 routing in Azure

377 Views Asked by At

App (web api) works fine locally. but in azure (App Service) request like http://myapp.azurewebsites.net/api/values returns error 404 (The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.) Routing is looking for physical path D:\home\site\wwwroot\api\values which doesn't exist.

in program.cs:

var host = new WebHostBuilder()
            .UseKestrel()
            .UseUrls("http://myapp.azurewebsites.net")
            .UseContentRoot(Directory.GetCurrentDirectory())
            .UseIISIntegration()
            .UseStartup<Startup>()
            .Build();

        host.Run();

in startup.cs:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddMvcCore(opts =>
         {
              opts.ModelBinderProviders.Insert(0, new CommaDelimitedArrayModelBinderProvider());
          });
    ...
}

web.config:

<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="flase" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
  </system.webServer>
</configuration>

update: error details

update 2: sub-error code is 0 (sc-substatus). sc-win32-status is 2 (file not found). I also enabled all logs, viewed them and didn't found any additional tips. Kudu Remote Execution Console didn't show any error and evidence of request at all. iis error

update 3: short version of api https://drive.google.com/open?id=1TUCImJSmuCC1HJK-x95wg5VOUUEB96wZ

3

There are 3 best solutions below

0
AjayKumar On

404 error has a many of sub-status code, to narrow down kindly find the sub-status code, you may review the Failed Request Tracing logs.

Make sure that the deployment files are available in wwwroot folder. Place the web.config file under the /site/wwwroot directory.

By default, on Azure WebApps, all files are stored in the file system with the application, including the media files. Refer to the article File structure on azure - https://github.com/projectkudu/kudu/wiki/File-structure-on-azure to know the sets of files & dirs on Azure WebApp.

To enable diagnostics in the Azure portal, go to the page for your web app and click Settings > Diagnostics logs.

Further, many startup errors don't produce useful information in the Application Event Log. You can run the app in the Kudu Remote Execution Console to discover the error: Checkout the steps mentioned in this document: https://learn.microsoft.com/en-us/aspnet/core/host-and-deploy/azure-apps/troubleshoot?view=aspnetcore-2.0#run-the-app-in-the-kudu-console

Reference: https://learn.microsoft.com/en-us/azure/app-service/web-sites-enable-diagnostic-log

4
Joey Cai On

I add a custom DateTime Model Binding in Core Web API and it is binding custom DateTime formats in API. I work well both in local and azure.

You could go this link to check. And use this link to format datetime. enter image description here

As @Ajay said, you could enable diagnostics log in azure portal.

Also, you could delete all the files in your KUDU to let the webapp rewrite files into it.

Or when you publish the app, click "Remove additional files at destination" to avoid you have make some changes but it not override. enter image description here

0
Mihail Katrikh On

Project was created in vs2015 and then migrated to vs2017. Obviously not all necessary changes were made in csproj-file. After rewriting it from the scratch problem was solved. I migrated to .net 2.0 also. Final version of csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp2.0</TargetFramework>
    <RootNamespace>ClearIt.Api</RootNamespace>
    <ApplicationIcon />
    <OutputType>Exe</OutputType>
    <StartupObject />
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="..\Dal\Dal.csproj" />
    <ProjectReference Include="..\Models\Models.csproj" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0" />
    <PackageReference Include="AutoMapper" Version="6.0.2" />
    <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="2.0.1" />
    <PackageReference Include="HtmlAgilityPack" Version="1.8.1" />
    <PackageReference Include="IdentityServer4.AccessTokenValidation" Version="2.0.0" />
    <PackageReference Include="Joonasw.AspNetCore.SecurityHeaders" Version="2.4.0" />
    <PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Hosting" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Formatters.Json" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="2.0.0" />
    <PackageReference Include="Microsoft.AspNetCore.WebUtilities" Version="2.0.0" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.0" />
    <PackageReference Include="SkiaSharp" Version="1.59.1" />
    <PackageReference Include="System.Text.Encoding.CodePages" Version="4.4.0" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
    <DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.4" />
  </ItemGroup>
  <ItemGroup>
    <None Update="appsettings.Development.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="appsettings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="appsettings.Production.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
    <None Update="Settings.job">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <None Update="web.config">
      <CopyToOutputDirectory>Never</CopyToOutputDirectory>
    </None>
  </ItemGroup>
</Project>