Home > Software design >  Cant find route, 404 any suggestions - C# .netcore
Cant find route, 404 any suggestions - C# .netcore

Time:02-20

Im trying to send the request to the route https://localhost:XXXX/Admins/products Im getting 404 error, i have been trying with various combinations to solve this problem. I cant find solution. Any help appreciated.

This is the controller file enter image description here

    [Route("[controller]")]
        [ApiController]
        public class AdminsController : Controller
        {
            private ApplicationDBContext _context;
    
            public AdminsController(ApplicationDBContext context)
            {
                _context = context;
            }
            
    
            [HttpGet("products")]
            public IActionResult GetProizvodi() => Ok(new UzmiProizvodeAdmin(_context).GetProizvodi());
    
            [HttpGet("products/{id}")]
            public IActionResult GetProizvod(int id) => Ok(new UzmiProizvodAdmin(_context).GetProizvod(id));
    
            [HttpPost("products")]
            public IActionResult KreirajProizvod(ModeliView.Proizvodi pv) => Ok(new KreirajProizvod(_context).Save(pv));
    
            [HttpDelete("products/{id}")]
            public IActionResult ObrisiProizvod(int id) => Ok(new ObrisiProizvod(_context).Delete(id));
    
            [HttpPut("products")]
            public IActionResult AzurirajProizvod(ModeliView.Proizvodi pv) => Ok(new AzurirajProizvod(_context).Update(pv));
}}

This is the Program.cs file enter image description here

var builder = WebApplication.CreateBuilder(args);

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

var configuration = builder.Configuration;
builder.Services.AddDbContext<ApplicationDBContext>(options => options.UseSqlServer(configuration.GetConnectionString("DefaultConnection")));

builder.Services.AddAuthorization();
builder.Services.AddAuthentication();

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();


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();

CodePudding user response:

You need to add app.MapControllers(); in your Program.cs

app.MapRazorPages();
// You missed this one.
app.MapControllers();

app.Run();

Works as expected

  • Related