Reputation: 12674
I've got my Startup.cs set up like so:
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace BotApiV2
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvcCore()
.AddApiExplorer();
services.AddRazorPages();
services.AddSwaggerGen();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
}
and my controller is set up like this:
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using BotApiV2.DataLayer;
namespace BotApiV2
{
[ApiExplorerSettings(IgnoreApi = false, GroupName = nameof(ValuesController))]
[Route("api/values")]
public class ValuesController : ControllerBase
{
[HttpGet]
[Route("test")]
public ActionResult Test()
{
var x = new BotDatabaseContext();
var t = new Dictionary<string, string>
{
{ "test1", "value1" },
{ "test2", "value2" },
{ "test3", "value3" }
};
return new JsonResult(t);
}
}
}
Browsing to /api/values/test
cannot be found however. Is there something set up wrong? I'm targeting .NET 5.0 (I've also tried .NET Core 3.1, same result).
Upvotes: 1
Views: 841
Reputation: 7190
You map the razor pages route in the startup. If you want your controller route to work, you need to map your controller route.
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
Upvotes: 3