.NET Core - 404 error on server only
Solution 1
I guess your routing is not done.try with your Url+/api/ballot.
Example: https://xxxx.azurewebsites.net/api/ballot. Here https://xxxx.azurewebsites.net is your azure url and api/ballot is your launch url defined in launchSettings.json file like "launchUrl": "api/ballot/",
[Route("api/[controller]")] should be in your controller class.
It should work.
Solution 2
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.
Related videos on Youtube
Jim S
Updated on June 04, 2022Comments
-
Jim S almost 2 years
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/" } } }
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.
-
Heretic Monkey about 6 yearsMicrosoft has published step-by-step instructions in excruciating detail here: docs.microsoft.com/en-us/aspnet/core/host-and-deploy/iis/… Have you followed all of those steps?
-
Jim S about 6 yearsThank you both for your help. I read about dotnet publish, and since it is the approved way, I changed to use it. I will edit the pubxml above to my new one. Unfortunately, I still get a 404. If you have other ideas, I would appreciate them.
-
Jim S about 6 yearsRunning from the command line has revealed additional information that it is a missing assembly. I have added the details to the end of the main post. I still cannot get it to run.
-
-
Jim S about 6 yearsThank you. I changed by web.config to look like yours, but I still get a 404.
-
Aspram about 6 yearsCould you send a screenshot or more detailed error that you get?
-
Aspram about 6 yearsAlso you can get logs by adding logs folder in your published files folder and changing web.config stdoutLogEnabled="true". This will generate files in your logs folder, where you can get more detailed error description and even stack trace.
-
Aspram about 6 yearsAnd also when creating IIS website, which physical path have you specified?
-
Jim S about 6 yearsI have added the exact message when running it from the command line, revealing that it is a missing assembly. Is that as good as a screenshot? The physical path is D:\wwwroot\api.iVoterGuide.com.
-
Aspram about 6 yearsI have a final suggestion, it seems to me this will help. Add this line to your web projects .csproj file, publish it again, more files will have to be generated I your publish folder after this step, the line to add is: <PublishWithAspNetCoreTargetManifest>false</PublishWithAspNetCoreTargetManifest> Add it at the end of <PropertyGroup> part.