Exclude controller from route with string param .NET core

328 Views Asked by At

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

There are 1 best solutions below

0
On BEST ANSWER

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+$)}")]