Git Product home page Git Product logo

Comments (18)

bufferUnderrun avatar bufferUnderrun commented on August 17, 2024 1

Hi,

I've the same need. I use the ninja patch below to support one wildcard in the middle of the url.

--- Unosquare.Labs.EmbedIO/Modules/WebApiModule.cs
+++ Unosquare.Labs.EmbedIO/Modules/WebApiModule.cs
@@ -32,11 +32,17 @@
                 var path = context.RequestPath();
                 var verb = context.RequestVerb();
                 var wildcardPaths = DelegateMap.Keys
-                    .Where(k => k.EndsWith("/" + ModuleMap.AnyPath))
+                    .Where(k => k.Contains("/" + ModuleMap.AnyPath))
                     .Select(s => s.ToLowerInvariant())
                     .ToArray();

-                var wildcardMatch = wildcardPaths.FirstOrDefault(p => path.StartsWith(p.Substring(0, p.Length - 1)));
+                var wildcardMatch = wildcardPaths.FirstOrDefault(
+                    p => // wildcard at the end
+                         path.StartsWith(p.Substring(0, p.Length - ModuleMap.AnyPath.Length))
+                         // wildcard in the middle so check both start/end
+                         || (path.StartsWith(p.Substring(0, p.IndexOf(ModuleMap.AnyPath)))
+                             && path.EndsWith(p.Substring(p.IndexOf(ModuleMap.AnyPath)+1)))
+                );

                 if (string.IsNullOrWhiteSpace(wildcardMatch) == false)
                     path = wildcardMatch;

A better solution could be to support regexp and map match group to argument of the method.
example :

[WebApiHandler(HttpVerbs.Get, RelativePath + "people/{id:([0-9]+)}")]
public bool GetPeople(WebServer server, HttpListenerContext context, int id) {
   ...
}

from embedio.

geoperez avatar geoperez commented on August 17, 2024

Can you post a sample where do you need the support to middle wilcard?

Thank you

On Thu, Mar 24, 2016 at 8:43 AM, Jon Ford [email protected] wrote:

Correct me if I'm mistaken, but it seems like the "*" wildcard is only
supported at the end of a URL rather than anywhere within it. Is this
intentional? Looking through the documentation, I don't see any examples of
a wildcard not at the end of a URL. Some of my endpoints require a wildcard
in the middle of a URL, and I'm sure other developers have the same
requirements.

β€”
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub
#28

Geo PΓ©rez

from embedio.

jondavidford avatar jondavidford commented on August 17, 2024

Sure. In my API, I have the concept of groups of devices. So, to get the nth group of devices, the endpoint that makes the most sense to me is /api/groups/n/devices.

Alternatives:
/api/groups/n
Doesn't make sense since groups have more properties than just devices.

/api/groups/devices/n
This is what I'm currently using as a workaround, but I'm not satisfied. Here's why: I also need an API endpoint the get the groups that are assigned to a device. If wildcard is supported in the middle of a URL, then my ideal endpoint is /api/devices/n/groups. Otherwise, I must use /api/devices/groups/n. Now an API user might be confused about which endpoint does what (/api/devices/groups/n vs. /api/groups/devices/n). When the wildcard is in the middle of the endpoint, the endpoint reads more like a language and we can deduce what it does. When the wildcard comes at the end of the endpoint, it is harder to tell whether the identifier applies to the first resource or the second (group or device).

I also have endpoints that require multiple wildcards, but that is a separate issue :)

from embedio.

mariodivece avatar mariodivece commented on August 17, 2024

Up-voted. I agree with Jon.

from embedio.

mariodivece avatar mariodivece commented on August 17, 2024

@bufferUnderrun would you be able to help us support regex-based routing without breaking wildcard routing by providing a pull request?

from embedio.

geoperez avatar geoperez commented on August 17, 2024

I commited the @bufferUnderrun suggestion with an Unit Test and it's seems to be fine. But I agreed to include better support with regex to routes and bind segments as arguments in the WebApi handler.

from embedio.

mariodivece avatar mariodivece commented on August 17, 2024

@geoperez the routing attribute needs to have the default constructor to use wildcard matching and add an additional constructor taking in a string and a RoutingStrategy enum that has the members Wildcard and RegEx

from embedio.

geoperez avatar geoperez commented on August 17, 2024

@mariodivece a very early version to support Regex and send args directly to the WebApi method is available in the next test file: https://github.com/unosquare/embedio/blob/master/Unosquare.Labs.EmbedIO.Tests/TestObjects/TestRegexController.cs

Let me know your thoughts

from embedio.

bufferUnderrun avatar bufferUnderrun commented on August 17, 2024

cool, i'll test this.

from embedio.

bufferUnderrun avatar bufferUnderrun commented on August 17, 2024

In your last commit, you use the new syntax introduced in C# 6.0 for property initializer.

Do you plan to retargeting to the most recent C# version ? because vs2013 does not support this...

edit : it seems for vs2013 you have to install the nuget package "Microsoft.Net.Compilers".

from embedio.

mariodivece avatar mariodivece commented on August 17, 2024

we are now using VS 2015/C# 6. The solution file and some areas of the codes are now starting to use the features provided by C# 6. We will however continue to target .net framework 4.5 as there is no apparent benefit for this project to target later versions.

from embedio.

mariodivece avatar mariodivece commented on August 17, 2024

@geoperez The implementation is incomplete. Please avoid publishing a nuget package until the async implementation is completed. Starting to look pretty good though! πŸ‘

from embedio.

bufferUnderrun avatar bufferUnderrun commented on August 17, 2024

@mariodivece hum, so after install vs2008 to maintain a old asp.net mvc1 project, vs2010 for a outlook addin and vs2013 for my current aspnet mvc5 projects, i will install vs2015... :) Hope embedio will rule them all πŸ‘

from embedio.

mariodivece avatar mariodivece commented on August 17, 2024

@bufferUnderrun Ha! Story of my life. @geoperez has it worse: He has to run VB6 on a VM to support some really old application.

from embedio.

geoperez avatar geoperez commented on August 17, 2024

😿

from embedio.

bufferUnderrun avatar bufferUnderrun commented on August 17, 2024

@geoperez you win !

from embedio.

geoperez avatar geoperez commented on August 17, 2024

@mariodivece async methods are available with Regex Strategic. I created more unit tests to check async implementation in both ways.

from embedio.

geoperez avatar geoperez commented on August 17, 2024

The new Nuget was published.

from embedio.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.