Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core wildcard route except

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"?

like image 559
severin Avatar asked Dec 13 '22 22:12

severin


1 Answers

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).*$)}")>]
like image 70
MindingData Avatar answered Dec 29 '22 13:12

MindingData