.NET Core - 404 error on server only

Jim S picture Jim S · Apr 6, 2018 · Viewed 11.8k times · Source

I have a very simple .NET Core test application that runs on my development machine but causes a 404 when run on my server under IIS 10. Initially, the goal was to return a few database records in JSON, like a web service. However, to narrow the field of search for the error, I have changed the application to return a single, constant string. The result is the same -- works on the dev machine and fails on the server.

On the dev machine, I ran it from Visual Studio 15.5.7. I published to the server using Web Deploy.

Based on other posts, I also tried setting "No Managed Code" for the IIS App Pool, but it made no difference.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace api.iVoterGuide.com.Controllers
{
    [Route("api/ballot")]
    public class BallotController: Controller {
        // GET api/value  --  ballot/542
        [HttpGet("{eleck}")]
        public IEnumerable<string> Get(short elecK)
        {
            yield return "[ 1, 2, 3]";
        }
    }
}

Here is Startup.cs

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

namespace api.iVoterGuide.com
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseMvc();
        }
    }
}

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.iVoterGuide.com
{
    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

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

web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
     <!-- It works either with or without this CORS code -->
     <system.webServer>
          <handlers accessPolicy="Read, Execute, Script" />
          <httpProtocol>
               <customHeaders>
                    <add name="Access-Control-Allow-Origin" value="*" />
               </customHeaders>
          </httpProtocol>
      </system.webServer>
</configuration>

Release.pubxml (EDITED)

<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <PublishProvider>FileSystem</PublishProvider>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <ProjectGuid>e86ba648-3c13-472c-b91c-1d0925762870</ProjectGuid>
    <publishUrl>bin\Release\PublishOutput</publishUrl>
    <DeleteExistingFiles>True</DeleteExistingFiles>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
</Project>

launchSettings.json

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:57342/",
      "sslPort": 0
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "api.iVoterGuide.com": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "api/values",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "applicationUrl": "http://localhost:57343/"
    }
  }
}

enter image description here

Does anyone have any suggestions? Thanks in advance.

EDIT: Following the advice from another article (which I can no longer find) I tried running my app from the command line.

D:\wwwroot\api.iVoterGuide.com>dotnet .\api.ivoterguide.com.dll
Error: An assembly specified in the application dependencies manifest (api.ivoterguide.com.deps.json) was not found:
package: 'Microsoft.ApplicationInsights.AspNetCore', version: '2.1.1'
path: 'lib/netstandard1.6/Microsoft.ApplicationInsights.AspNetCore.dll'
This assembly was expected to be in the local runtime store as the application was published using the following target manifest files:
aspnetcore-store-2.0.0-linux-x64.xml;aspnetcore-store-2.0.0-osx-x64.xml;aspnetcore-store-2.0.0-win7-x64.xml;aspnetcore-store-2.0.0-win7-x86.xml

Here is the --info result on the server:

D:\wwwroot\api.iVoterGuide.com>dotnet --info
Microsoft .NET Core Shared Framework Host

Version  : 2.0.6
Build    : 74b1c703813c8910df5b96f304b0f2b78cdf194d

Even with this information I have not been able to solve the problem. I have tried installing the newest .NET version, changing the .NET Core version in my csproj (), and several other small changes suggested by other articles.

I still cannot get to run. Any suggestions.

Answer

Jim S picture Jim S · Apr 17, 2018

The solution was to install .Net Core Windows Server Hosting on the server, as described here. It can be downloaded here.

It was not necessary to set PublishWithAspNetCoreTargetManifest to false in the .csproj file.

I can either publish from Visual Studio or using the command line (dotnet publish -c Release -o bin\PublishOutput). The latter is much faster.