0

Trying to implement Scalar in a .net9 aspcore app.

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

Which leads me to believe something is wrong with my code or Installation of scalar. Any Ideas?

Saclar should show the UI with my exposed API methods

2
  • can you share your configuration for Scalar here? to Understand what is the issue Commented Feb 18 at 4:57
  • default URL for OpenAPI documentation URL is this localhost:7160/openapi/v1.json Commented Feb 18 at 5:22

2 Answers 2

1

I guess you‘re calling /scalar/v1/ in your browser. Please try it again without the trailing slash /scalar/v1.

Sign up to request clarification or add additional context in comments.

Comments

0

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:

result

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.