Mvc: Regexes in attribute route

Created on 4 Aug 2015  路  2Comments  路  Source: aspnet/Mvc

What am I doing wrong here? I have this route in a site that uses an older attribute routing library:

[GET(@"B{alias:regex(^[0-9A-Za-z\-_]+$)}", ControllerPrecedence = -1)]

I'm trying to upgrade the site to ASP.NET 5. This was my first attempt at doing so:

[Route(@"B{alias:regex(^[0-9A-Za-z\-_]+$)}", Order = 99)]
public virtual ActionResult ShortUrl(string alias)
{

Except it crashes with an error:

For action: 'Daniel15.Web.Controllers.BlogController.ShortUrl'
Error: While processing template 'B{alias:regex(^[0-9A-Za-z\-_]+$)}', a replacement value for the token '0-9A-Za-z\-_' could not be found. Available tokens: 'action, controller'.

Stack:

Microsoft.AspNet.Mvc.ControllerActionDescriptorBuilder.Build(ApplicationModel application)
Microsoft.AspNet.Mvc.Core.ControllerActionDescriptorProvider.GetDescriptors()
Microsoft.AspNet.Mvc.Core.ControllerActionDescriptorProvider.OnProvidersExecuting(ActionDescriptorProviderContext context)
Microsoft.AspNet.Mvc.Core.DefaultActionDescriptorsCollectionProvider.GetCollection()
Microsoft.AspNet.Mvc.Core.DefaultActionDescriptorsCollectionProvider.get_ActionDescriptors()
Microsoft.AspNet.Mvc.Routing.AttributeRoute.GetInnerRoute()
Microsoft.AspNet.Mvc.Routing.AttributeRoute.RouteAsync(RouteContext context)
Microsoft.AspNet.Routing.RouteCollection.<RouteAsync>d__9.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
Microsoft.AspNet.Builder.RouterMiddleware.<Invoke>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
System.Runtime.CompilerServices.TaskAwaiter.GetResult()
Microsoft.AspNet.Diagnostics.ErrorPageMiddleware.<Invoke>d__5.MoveNext()
question

Most helpful comment

Solved - The [ and ] need to be escaped as [[ and ]].

All 2 comments

Solved - The [ and ] need to be escaped as [[ and ]].

Thanks solved it!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

karthicksundararajan picture karthicksundararajan  路  4Comments

LiangZugeng picture LiangZugeng  路  3Comments

kspearrin picture kspearrin  路  4Comments

CezaryRynkowski picture CezaryRynkowski  路  4Comments

hikalkan picture hikalkan  路  4Comments