This is essentially what I have, a very simple set of three files with fresh asp.net core 2.1 (actually copy-pasted from tutorials):
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
Then goes the simplest startup
class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseMvc();
}
}
And default values controller
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2" };
}
}
No matter what I call I see in console same 404 error:
Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET http://localhost:5000/values
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 105.0181ms 404
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
Request starting HTTP/1.1 GET http://localhost:5000/api/values
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 2.6016ms 404
etc
I tried adding default route both with app.UseMvcWithDefaultRoute();
and specifying it manually. I tried removing route attributes when used the default route. Also tried adding AddControllersAsServices()
. But result is still same - 404. When I setup custom handler in app.Run
then it works without any issues.
csproj (I have replaced default Microsoft.AspNetCore.All dependency, but routing still does not work)
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.0" />
</ItemGroup>
</Project>
The Route attribute can be applied on any controller or action method. In order to use attribute routing with Web API, it must be enabled in WebApiConfig by calling config. MapHttpAttributeRoutes() method.
Enabling Attribute Routing To enable attribute routing, call MapHttpAttributeRoutes during configuration. This extension method is defined in the System. Web. Http.
Self Hosting. You can host a Web API as separate process than ASP.NET. It means you can host a Web API in console application or windows service or OWIN or any other process that is managed by . NET framework.
You can self-host a web API in your own host process. New applications should use OWIN to self-host Web API. See Use OWIN to Self-Host ASP.NET Web API 2.
For me it helped adding AddApplicationPart
after AddMvc
like this:
.AddMvc()
.AddApplicationPart(typeof(Startup).Assembly)
This was rather difficult to track down, but the problem boils down to this in your .csproj:
<Project Sdk="Microsoft.NET.Sdk">
As you are building a web application, you need to instead reference the Web Sdk, as follows:
<Project Sdk="Microsoft.NET.Sdk.Web">
I managed to reproduce and fix your issue with this small change.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With