0

I want to have an endpoint that looks like: localhost:5000/abc123

This is basically to replicate the functionality of tinyurl.

Controller

[HttpGet("/{myString}")]
public async Task<IActionResult> Get(string myString)
{}

This works but all files now come through this contoller eg: localhost:5000/runtime.js etc

Is this possible to do only for certain strings?

  • 1
    Is there a pattern for URLs that are supposed to match `[HttpGet("/{myString}")]` ? E.g. 3 letters + 3 numbers like in your question. – Roar S. Jul 15 '21 at 20:25
  • Why not put the files into folders under wwwroot?The default wwwroot will have css,js,lib folders. – Yiyi You Jul 16 '21 at 03:03

1 Answers1

0

Use Route constraint to filter values for myString

For example, if a file name is a string containing a dot . is a valid suggestion in your case, you can use the following regex to accept alphanumeric strings

[HttpGet("/{myString::regex(^\\w+$)}")]
FireAlkazar
  • 1,795
  • 1
  • 14
  • 27