Doing the basics by using OpenApi. When i run it I see the UI but i
dont see any of my methods. In the output window i see Request
finished HTTP/2 GET
https://localhost:7160/scalar/v1/scalar.aspnetcore.js - 404 0 -
38.9624ms
The issue might relate the request URL.
By default, when using Scalar for interactive API documentation and to view the Scalar UI, the request URL is https://localhost:<port>/scalar/v1: (you can find the port from Properties\launchSettings.json file)
To configure Scalar, we need to install the Scalar.AspNetCore package, and the Program.cs file should like this: we need to add app.MapScalarApiReference();:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
app.MapScalarApiReference();
}
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
};
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.MapProductEndpoints();
app.Run();
Besides, please make sure you add the API controller or endpoints:
controller:
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET: api/<ValuesController>
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
// GET api/<ValuesController>/5
[HttpGet("{id}")]
public string Get(int id)
{
return "value";
}
endpoints:
public class Product
{
public int ID { get; set; }
public string Name { get; set; }
}
public static class ProductEndpoints
{
public static void MapProductEndpoints (this IEndpointRouteBuilder routes)
{
var group = routes.MapGroup("/api/Product").WithTags(nameof(Product));
group.MapGet("/", () =>
{
return new [] { new Product() };
})
.WithName("GetAllProducts")
.WithOpenApi();
group.MapGet("/{id}", (int id) =>
{
//return new Product { ID = id };
})
.WithName("GetProductById")
.WithOpenApi();
Then the Scalar view as below:
