0

I just have one quick question about what seems to have been a limitation with ASP.NET Web API Attribute Routing, but hoping I just suck at research. In my controller, I'm trying to do something like this:

public class OrdersController : ApiController { [HttpGet] [Route("{apiRoot}/customers/{id:int}/orders")] public IHttpActionResult GetCustomerOrders(int id) {...} } 

Where {apiRoot} is defined in either a configuration file.

This may not actually be necessary, but I'd like to know how to put a specific path in the route attribute without having to code a static path. Is the general idea here supposed to be that you only put text into the route path, except for your parameters which go in {}?

0

1 Answer 1

1

How about switching to using a RoutePrefix:

[MyRoutePrefix] public class OrdersController : ApiController { [HttpGet] [Route("customers/{id:int}/orders")] public IHttpActionResult GetCustomerOrders(int id) {...} } public class MyRoutePrefixAttribute : RoutePrefixAttribute { public MyRoutePrefixAttribute() { Prefix = "the route prefix"; } } 

RoutePrefixAttribute isn't sealed like RouteAttribute so extending it should allow you do what you need. Assuming, of course, that all of the controllers in a single class using the same root path.

Note: I haven't had a chance to try this but given what I know of attribute routing, I don't see why it shouldn't work.

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

1 Comment

Turns out that just using the RoutePrefix attribute by itself works great. I didn't want to have to inherit RoutePrefix, but just decorating the class with the attribute and path works great. I could have probably fixed it via inheritance, but just using RoutePrefix works fine. Thanks for the answer!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.