1

I have small API which serves files. The requested file is coming from our ClickOnce app. Instead of multiple GET actions which will go into the requested folder/file, is it possible to have single GET action which from I can get all the route parameters so I can build my relative path.

Example GET routes

/api/ApplicationFiles
/api/ApplicationFiles/somefile
/api/ApplicationFiles/someFolder/somefile
/api/ApplicationFiles/someFolder/someFolder/someFolder/someFile

So I have to define multiple GET endpoints.

1
  • 1
    First can you please show the code.. a fully reproducible example and the actual output plus the expected output? Second have you thought of just using Query String parameters instead? Commented Aug 3, 2020 at 8:21

2 Answers 2

6

You can use an asterisk to indicate that your route parameter can contain slashes. This is called a catch-all parameter, and looks like this:

[Route("/api/ApplicationFiles/{**path}")]
public IActionResult GetFile(string path)
{
    // code
}

From the documentation:

You can use an asterisk (*) or double asterisk (**) as a prefix to a route parameter to bind to the rest of the URI. These are called a catch-all parameters. For example, blog/{**slug} matches any URI that starts with /blog and has any value following it, which is assigned to the slug route value. Catch-all parameters can also match the empty string.

The catch-all parameter escapes the appropriate characters when the route is used to generate a URL, including path separator (/) characters. For example, the route foo/{*path} with route values { path = "my/path" } generates foo/my%2Fpath. Note the escaped forward slash. To round-trip path separator characters, use the ** route parameter prefix. The route foo/{**path} with { path = "my/path" } generates foo/my/path.

Sign up to request clarification or add additional context in comments.

Comments

1

It's possible to define multiple route attributes for a single controller method. Have a look at this example: https://learn.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-3.1#attribute-routing-for-rest-apis

[Route("~/api/ApplicationFiles")]
[Route("~/api/ApplicationFiles/{somefile}")]
[Route("~/api/ApplicationFiles/{someFolder}/{somefile}")]
[Route("~/api/ApplicationFiles/{someFolder}/{someFolder2}/{someFolder3}/{somefile}")]

where somefile, someFolder, someFolder2, and someFolder3 are string parameters of your controller method

1 Comment

Yes, this is what I'm trying to get rid of.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.