0

I am building a WebApi v.2 method for the following route:

[Route("api/{entityName}/logo/{entityId}/{size:char}")]
public HttpResponseMessage GetLogo(string entityName, string entityId, char size) {...}

Now, I need to restrict size parameter to one of three characters: s, m, l. If size is not one of those, I don't want this route to be matched.

Is it possible?

1 Answer 1

2

I am thinking that something like this may work

  [Route("api/{entityName}/logo/{entityId}/{size:regex(^[sml]?$)}")]

You can add a contstraint to a direct route using Shorthand References for Constraint Classes Used in Direct Route Templates.

A full list of the shorthand direct route regular expression classes can be found here

http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2#constraints

Stack overflow previous question similar topic Regex in Route attribute - RESTful API ASP.NET Web API

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

1 Comment

That's awesome, exactly what I was looking for. Thank you!

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.