I asked a similar question a few days ago, but I've got an issue with another controller now and for the life of me I can't figure out why this is returning a 404.
API Controller...
[Route("api/[controller]")]
[ApiController]
public class FilesController
{
private readonly IFilesService _filesService;
public FilesController(IFilesService filesService)
{
_filesService = filesService;
}
[HttpGet("{id}")]
public IEnumerable<SupportFile> GetFiles(int id) {
return _filesService.GetFiles(id);
}
[HttpGet("DownloadFile/{fileName}")]
public async Task<FileStreamResult> DownloadFile(string fileName)
{
return await _filesService.DownloadFile(fileName);
}
}
Program.cs...
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddSingleton<DapperContext>();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<ISupportService, SupportService>();
builder.Services.AddScoped<IFilesService, FilesService>();
builder.Services.AddControllersWithViews();
builder.Services.AddControllers();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
// 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.MapControllers();
app.UseRouting();
app.MapDefaultControllerRoute();
app.MapFallbackToFile("index.html"); ;
app.Run();
Proxy.conf.js...
const { env } = require('process');
const target = env.ASPNETCORE_HTTPS_PORT ? `https://localhost:${env.ASPNETCORE_HTTPS_PORT}` :
env.ASPNETCORE_URLS ? env.ASPNETCORE_URLS.split(';')[0] : 'http://localhost:19229';
const PROXY_CONFIG = [
{
context: [
"/api/*"
],
target: target,
secure: false,
headers: {
Connection: 'Keep-Alive'
}
}
]
module.exports = PROXY_CONFIG;
Another api controller that works fine...
[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
private readonly IUserService _userService;
public UsersController(IUserService userService)
{
_userService = userService;
}
[HttpPost]
public User ModifyUser(AzureUser user)
{
return _userService.ModifyUser(user);
}
[HttpGet]
public IEnumerable<User> GetUsers()
{
return _userService.GetUsers();
}
}
But no matter what I do...
https://localhost:44427/api/files/123 returns a 404.
https://localhost:44427/api/files/DownloadFile/test.csv returns a 404.
Other API controller methods work, so the port is correct.
The other api controllers only have one GET though, whenever I try to add multiple GETs using attribute routing, they both just end of returning a 404.
CodePudding user response:
Solved this.
It was the proxy.conf.js.
I had to change "/api/*" to "/api/**" in the context array, ** seems to be a catch all.
CodePudding user response:
Your controller should inherit the class ControllerBase
.
Instead of this:
public class FilesController
Try this:
public class FilesController : ControllerBase
CodePudding user response:
Have you tried debugging?
Does a file with the ID 123 or name test.csv actually exist?
https://localhost:44427/api/files/123
https://localhost:44427/api/files/DownloadFile/test.csv
If not then then the service methods may return null which will lead to a 404 by default.