Aspnetcore: Web API project template works only as Web App, does not work as Console App

Created on 9 Jan 2019  路  1Comment  路  Source: dotnet/aspnetcore

Describe the bug

If I take a working ASP.NET Core Web App project and copy the code to a new .NET Core Console App project, my web API no longer works.

This is unexpected, since ASP.NET Core is just a library - I do not see why its behavior should be driven by project type.

This is also undesirable, as I want my code to be what affects my app behavior, not some SDK magic.

To Reproduce

Steps to reproduce the behavior:

  1. On .NET Core 2.2 with <PackageReference Include="Microsoft.AspNetCore.App" /> whatever that version is (latest).
  2. Create a new ASP.NET Core Web App project, "Web API" template in Visual Studio 15.9.5. Run and verify that ValuesController.Get() returns values in browser.
  3. Create a new .NET Core Console App project.
  4. Copy all the code/config files from Web App to Console App project, overwrite Program.cs.
  5. Add Microsoft.AspNetCore.App reference to Console App project.
  6. Run Console App. It will by default listen for HTTP on port 5000.
  7. Open http://localhost:5000/api/Values

Expected behavior

The same ValuesController.Get() result.

Actual behavior

GET http://localhost:5000/api/Values HTTP/1.1
User-Agent: Fiddler
Host: localhost:5000


HTTP/1.1 404 Not Found
Date: Wed, 09 Jan 2019 11:34:19 GMT
Server: Kestrel
Content-Length: 0

Log (level Trace) says:

info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/api/Values
dbug: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[0]
      Wildcard detected, all requests with hosts will be allowed.
trce: Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[0]
      All hosts are allowed.
dbug: Microsoft.AspNetCore.Routing.Matching.DfaMatcher[1000]
      No candidates found for the request path '/api/Values'
dbug: Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware[2]
      Request did not match any endpoints
dbug: Microsoft.AspNetCore.Server.Kestrel[9]
      Connection id "0HLJM4R6D0RD6" completed keep alive response.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
      Request finished in 12.5322ms 404

Additional info

Simply alternating between these two lines is enough to break/fix the behavior:

<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk">
By Design area-mvc question

>All comments

Thank you for your feedback, @sandersaares.
The issue you're facing is by-design. It's required for a project to reference the correct SDK for things to work, when using ASP.NET Core.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

BrennanConroy picture BrennanConroy  路  3Comments

UweKeim picture UweKeim  路  3Comments

farhadibehnam picture farhadibehnam  路  3Comments

rbanks54 picture rbanks54  路  3Comments

FourLeafClover picture FourLeafClover  路  3Comments