简体   繁体   中英

Exclude controller from route with string param .NET core

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM