Reputation: 5952
I am trying to learn asp.net core so according official site i installed it's sdk and create simple web api by this command:
dotnet new webapi -o TodoApi
My ide is vscode. This is my controller class :
namespace TodoApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/values/5
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
return "value";
}
[HttpGet("api/cities")]
public JsonResult GetCities(){
return new JsonResult(new List<object>(){
new {id=1, name = "new york city"},
new {id = 2 , name = "gorgan"}
});
}
}
}
I added GetCities
method in this example.In postman when i use
https://localhost:5001/api/values
I got result :
[
"value1",
"value2"
]
but when i call my method that i have already added to controller, in postamn
https://localhost:5001/api/cities
I got 404 Not Found
and i have to use this url https://localhost:5001/api/values/api/cities
to got right answer?
What is a problem? and how could i fix that?
This is setting:
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://localhost:34842",
"sslPort": 44370
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": false,
"launchUrl": "api/values",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"TodoApi": {
"commandName": "Project",
"launchBrowser": false,
"launchUrl": "api/values",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
And dotnet info:
groot@groot-Product:~$ dotnet --info
.NET Core SDK (reflecting any global.json):
Version: 2.1.700
Commit: c2ef055a0f
Runtime Environment:
OS Name: ubuntu
OS Version: 18.04
OS Platform: Linux
RID: ubuntu.18.04-x64
Base Path: /usr/share/dotnet/sdk/2.1.700/
Host (useful for support):
Version: 2.1.11
Commit: d6a5616240
.NET Core SDKs installed:
2.1.700 [/usr/share/dotnet/sdk]
.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.11 [/usr/share/dotnet/shared/Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.11 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.11 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
To install additional .NET Core runtimes or SDKs:
https://aka.ms/dotnet-download
groot@groot-Product:~$
Upvotes: 2
Views: 4104
Reputation: 889
Your Controller name is ValuesController. ASP.Net uses the controller name for the path. So in this case you should call
api/values/GetCities
and remove the
[HttpGet("api/cities")]
attribute from your method.
If you want the path to be
GET api/cities
Create a new controller, and copy your code into that controller renaming your method to Get()
then sit back and let the code do the routing for you.
Upvotes: 2