Wysyłanie żądań w Postmanie

0

Wpisuje w Postmanie następujący URL dla metody Get: https://localhost:7033/api/restaurant. Niestety jako rezultat dostaje 404 not found.

Oto kod kontrolera:

[Route("api/restaurant")]
public class RestaurantController : ControllerBase
{
    private readonly RestaurantDbContext _dbContext;

    public RestaurantController(RestaurantDbContext dbContext)
    {
        _dbContext = dbContext;
    }

    [HttpGet]
    public ActionResult<IEnumerable<Restaurant>> GetAll()
    {
        var restaurants = _dbContext.Restaurants.ToList();

        return Ok(restaurants);
    }
}

oraz kod pliku launchSettings:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:8196",
      "sslPort": 44311
    }
  },
  "profiles": {
    "RestaurantApp": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:7033;http://localhost:5033",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}
0

a w przeglądarce działa?

0

W przeglądarce też nie działa.

0

dla https://localhostlocalhost:7033 działa poprawnie, więc musi być coś nie tak z [Route("api/restaurant")]

0

A spróbuj strzelić na ten adres http (5033)

0

Pokaż Startup.cs, może tam masz jakoś customowo zmapowane kontrolery?

0

Nie ma w tym projekcie pliku Startup.cs

0

A może być coś nie tak z kontrolerem?

0

Taka jest zawartość Program.cs:

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddRazorPages();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapRazorPages();

app.Run();
2

Brakuje Ci app.Services.AddControllers() i app.MapControllers(). Wziąłeś projekt RazorPages, a oczekujesz, że kontrolery magicznie zadziałają same z siebie.

0

@Saalin: Nie mogę dodać app.Services.AddControllers(). Nie ma takiej metody AddControllers(). Wiesz może jak to rozwiązać?

1

przed
var app = builder.Build();
dodaj:
builder.Services.AddControllers();

0

@kzkzg: No dobra. Dodałem to a kontrolery dalej nie działają. Nadal dostaje status 404 not found.

0
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

dodaj jeszcze przed app.Run(); dla mvc

albo dla web api

app.MapControllers();

1 użytkowników online, w tym zalogowanych: 0, gości: 1