0

I have the following routes set up in my webapi..

        //Route for getting the api access token
        config.Routes.MapHttpRoute(
            name: "AccessTokenRoute",
            routeTemplate: "api/access-token",
            defaults: new { controller = "AccessToken" }
        );

        //Default route for the api
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

I have a controller called AccessTokenController. When a request comes in with the url /api/access-token, everything works fine. It hits the appropriate controller.

My problem is that if a request comes in at /api/AccessToken, it will also match the default route. I want to prevent the route from matching if they access it that way. Is there anything that exists in the routing system to do this?

1 Answer 1

1

Add a constraint to your DefaultApi route that restrains the access if the controller is AccessToken.

Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to do that with the constraint. I have tried setting controller to the following regex with no success. .... constraints: new { controller = @"[^AccessToken]" }
I'm not good at using regex, so I created a class that implements IRouteConstraint and it worked.
try constraints: new { controller = @"^(?!.*accesstoken).*$" }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.