找不到路线,404 任何建议 - C# .net 核心
Cant find route, 404 any suggestions - C# .netcore
我正在尝试将请求发送到路由 https://localhost:XXXX/Admins/products
我收到 404 错误,我一直在尝试各种组合来解决这个问题。
我找不到解决方案。任何帮助表示赞赏。
这是控制器文件
[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));
}}
这是 Program.cs 文件
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();
您需要在 Program.cs
中添加 app.MapControllers();
app.MapRazorPages();
// You missed this one.
app.MapControllers();
app.Run();
我正在尝试将请求发送到路由 https://localhost:XXXX/Admins/products 我收到 404 错误,我一直在尝试各种组合来解决这个问题。 我找不到解决方案。任何帮助表示赞赏。
这是控制器文件
[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));
}}
这是 Program.cs 文件
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();
您需要在 Program.cs
app.MapControllers();
app.MapRazorPages();
// You missed this one.
app.MapControllers();
app.Run();