简体   繁体   中英

Routing with regular expressions in ServiceStack

I'm trying to build a small .NET Core server with ServiceStack and would like to use regular expressions for routing. So far I've basically just created a Hello World project and connected it to database.

I have these classes in my ServiceModel project:

[Route("/hello/{Language}/{Name*}", Matches = @"PathInfo =~ \/hello\/[a-z]{2}\/[A-Za-z]+$")]
public class HelloTo : IReturn<HelloResponse> {
    public string Language { get; set; }
    public string Name { get; set; }
}

[Route("/hello/{Language*}", Matches = @"PathInfo =~ \/hello\/[a-z]{2}$")]
public class Hello : IReturn<HelloResponse> {
    public string Language { get; set; }
}

public class HelloResponse {
    public string Result { get; set; }
}

and this in my ServiceInterface project:

public HelloResponse Any(HelloTo request) {
    var greeting = Db.SingleById<Greeting>(request.Language);
    return new HelloResponse { Result = $"{greeting.Text}, {request.Name}!" };
}

public HelloResponse Any(Hello request) {
    var greeting = Db.SingleById<Greeting>(request.Language);
    return new HelloResponse { Result = $"{greeting.Text}, you!" };
}

The point is, when I send eg this request: http://localhost/hello/fr , it goes to the first route, even though it has no Name in it.

As far as I can tell, the second route is inaccesible with this configuration. After some experimentation, it seems to me as if the Matches parameter is completely ignored, which makes me think that maybe I need to enable something. However, I couldn't find much documentation on this, besides a short note here that routing using regexps should be possible somewhat like this.

Note that if I changed the second route from Route("/hello/{Language*}" to Route("/hello/{Language}" , this sample will work as expected, presumably because routes without wildcards take precedence, but I need routes ending with wildcards like this for it to be of any practical use.

此问题是由于未针对通配符路由验证匹配规则而导致的,现在可以通过此提交解决匹配,在此提交中 ,您的示例将按预期从MyGet上可用的 ServiceStack v5.0.3中正常工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM