1

In the .NET Core Web API I have written a controller for accessing files (for authorization purpose).

The controller simply takes a path, reads the file at the path and returns the file (image) in a way the browser can display it.

The controller looks like

[HttpGet("{path}", Name = "GetFile")]
public IActionResult GetFile(string path)
{
  // read the file & content type
  return File(f, contentType);
}

Everything works fine if I use only filenames (Files is the controller), e.g.

http://localhost:61261/api/v1/Files/test.png

The problem is, when I use subdirectories the routing fails with 404 and the controller method isn't called. when e.g.

http://localhost:61261/api/v1/Files/subdirectory/test.png

How would I have to do the routing that in the 2nd example the path variable would be "subdirectory/test.png"?

Edit: I am looking for some sort of wildcard since subdirectories are can come and go as files are uploaded/downloaded. And using static files is not a possibility because they are public (no way to use authentication/authorization and the files are not in the wwwwroot.

monty
  • 7,888
  • 16
  • 63
  • 100

1 Answers1

0

To extract the relevant info from the possible duplicate question:

Adding an asterisk in front of path solved the problem

[HttpGet("{*path}", Name = "GetFile")]
monty
  • 7,888
  • 16
  • 63
  • 100