You need to configure your startup to support web api and attribute routing.
services.AddControllers()
adds support for controllers and API-related features, but not views or pages. Refer to MVC service registration.
Add endpoints.MapControllers
if the app uses attribute routing. Refer to Migrate MVC controllers.
Combine razor pages and api like:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
});
services.AddRazorPages()
.AddNewtonsoftJson();
services.AddControllers()
.AddNewtonsoftJson();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//other middlewares
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapControllers();
});
}