Routing is not working with self-hosted web API

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.


you can try change code to this:

[ApiController]
public class ValuesController : ControllerBase
{
    [HttpGet]
    [Route("api/values")]
    public ActionResult<IEnumerable<string>> Get()
    {
        return new string[] { "value1", "value2" };
    }
}

The atribute Route can use for asigning an specific route to a function on api or view.

then to call you can use:

'localhost:5000/api/values'