I'm using ASP.NET Core to serve an SPA like this:
[<Route("{*url}")>]
member this.Index () =
this.View("~/wwwroot/Index.cshtml")
My JSON endpoints are on the same app, routed like this:
[<Route("api/something/{somethingId:int})>]
This works fine, except that when I try to call an endpoint api/something/that/doesnt/exist
I get the index page when I actually want a 404.
Is it possible to setup [<Route("{*url}")>]
in such a way that it excludes any url that starts with "api"?
You should use Route Constraints. There is a regex option which you can use to filter out the API calls (That you don't want).
You then need to do a negative lookahead to negate a string.
[<Route("{*url:regex(^(?!api).*$)}")>]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With