Skip to content
This repository has been archived by the owner on Nov 27, 2018. It is now read-only.

Using square brackets in regex constraints #373

Closed
vaindil opened this issue Dec 17, 2016 · 5 comments
Closed

Using square brackets in regex constraints #373

vaindil opened this issue Dec 17, 2016 · 5 comments
Assignees

Comments

@vaindil
Copy link

vaindil commented Dec 17, 2016

I have a controller that accepts either an ID or a string at a given endpoint. I have the ID function set correctly using api/departments/{id:long}, but I cannot figure out how to route to the string endpoint. The alpha constraint is not enough as the string can contain numbers.

I'm trying to use this route to accept any valid string but require that the first character be a letter.

[Route("api/departments/{deptName:regex(^[a-zA-Z]{1}[a-zA-Z0-9_]*$)}")]

This does not work, however:

For action: 'Namespace.DepartmentsNameController.Get (Namespace)'
Error: While processing template 'api/departments/{deptName:regex(^[a-zA-Z]{1}[a-zA-Z0-9_]*$)}', a replacement value for the token 'a-zA-Z' could not be found. Available tokens: 'action, controller'.

What is the correct way to approach this?

@dougbu
Copy link
Member

dougbu commented Dec 18, 2016

Escape (double) the special characters: ^[[a-zA-Z]]{{1}}[[a-zA-Z0-9_]]*$. Since matches are not case-sensitive and {1} is unnecessary, ^[[a-z]][[a-z0-9_]]*$ works too.

That said, we should improve the message in this case. An un-escaped curly brace leads to an informative message:

In a route parameter, '{' and '}' must be escaped with '{{' and '}}'

AttributeRouteModel currently ignores curly braces when handling square brackets and the

An unescaped '[' token is not allowed inside of a replacement token. Use '[[' to escape.

message appears only when square brackets appear within square-bracketed tokens.

Nit: The curly-brace message should end with a period 😈

@vaindil
Copy link
Author

vaindil commented Dec 18, 2016

Oh that's awesome. Not sure how I looked at it so many times and didn't see the {1} being unnecessary, heh, and I didn't realize even regex matches are not case-sensitive for routing. Thank you so much, especially for helping on a Saturday night!

Would it be helpful to include this in the routing docs? I can open an issue if it would help. I scoured that page to find the info; this issue was my last resort.

@Eilon Eilon added this to the 2.0.0 milestone Dec 29, 2016
@Eilon
Copy link
Member

Eilon commented Dec 29, 2016

Yeah definitely something we can improve.

I think the fixes are:

  • Add a period to the end of the In a route parameter, '{' and '}' ... message
  • For the While processing template 'foo', a replacement value ... message, add a blurb about how to escape [ and ]. Use something similar (but not identical) to the An unescaped '[' token message (because you can't know at this time that it's actually unescaped, so it's just informative, not accusative).

At the same time, I'm working on some improvements to the routing docs to clarify some of this.

@Eilon
Copy link
Member

Eilon commented Dec 29, 2016

Here's my PR to improve the docs if anyone wants to take a look: dotnet/AspNetCore.Docs#2450

@kichalla
Copy link
Member

kichalla commented Jan 5, 2017

755d436

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants