Reputation: 33
I would like to have second controller in my asp.net WebApi, but when i add it it not works... First Controller works OK
i have 404 not found in my browser
whats wrong?
namespace testing.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering",
"Scorching"
};
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger)
{
_logger = logger;
}
[HttpGet]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
}
and the second is below
{
[Route("[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpGet]
public int Get()
{
return 100050;
}
}
}
Can some one tell me whats wrong?
Upvotes: 0
Views: 586
Reputation: 156
There is another web server here that will forward request /weatherforecast to kestrel web server which host your controller.
Edit /ClientApp/setupProxy.js, change /weatherforecast
to /api/**
const context = [
"/api/**",
];
Edit Program.cs, add api/
to the path
app.MapControllerRoute(
name: "default",
pattern: "api/{controller}/{action=Index}/{id?}");
Edit MyController.cs, add api/
to route
[ApiController]
[Route("api/[controller]")]
public class MyController : ControllerBase
Alternatively, you could remove the SpaProxy and serve the static pages directly from the kestrel web server.
Upvotes: 0
Reputation: 17579
You're using the attribute [Route("[controller]")]
on your controller class. The string [controller]
means "the name of the class, without the actual WORD "Controller".
This means, the name of the controller is "Values" (or "WeatherForecast" for the previous controller).
So, the final url route you want is /Values
, not /ValuesController
.
You can read more about how this works on the MS Docs page (the whole page has a lot of good information, not just that section).
Upvotes: 2