Reputation: 679
My endpoint routing is not working in my asp.net core 3.0 Api
. I have seen similar questions but I am still not sure what is missing here.
I have the following in Startup.cs
{
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.AddControllers()
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
.AddNewtonsoftJson(options =>
{
options.SerializerSettings.Formatting = Formatting.Indented;
options.SerializerSettings.Converters.Add(
new Newtonsoft.Json.Converters.StringEnumConverter());
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
app.UseDeveloperExceptionPage();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
"test",
"api/v1.0/{controller}/{id?}");
});
}
}
My Ping controller
looks like:
public class PingController : ControllerBase
{
public IActionResult Get()
{
return Ok(true);
}
}
navigating to http://localhost//api/v1.0/Ping returns 404 page not found.
What am I missing here ? I also saw that MS suggests attribute routing for Web Apis but wanted to figure out why this is not working in the first place.
Upvotes: 0
Views: 5757
Reputation: 545
Follow this to add additional routing in .Net Core 3.1
endpoints.MapControllerRoute(
name: "prod", pattern: "product/item", new { controller="Product", action="Index"});
Upvotes: 1
Reputation: 679
@rfcdejong was correct, it was missing {action}
token.
The following correction fixed the problem.
endpoints.MapControllerRoute( "test", "api/v1.0/{controller}/{action=Get}/{id?}");
Upvotes: 1