Routing is not working with self-hosted web API [duplicate]

江枫思渺然 提交于 2019-11-27 22:27:42

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.

user2959372

For me it helped adding AddApplicationPart after AddMvc like this:

.AddMvc()
.AddApplicationPart(typeof(Startup).Assembly)

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'

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!