-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify WebApi to support middle wildcard, fix by @bufferUnderrun
- Loading branch information
Showing
3 changed files
with
63 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ public class Person | |
|
||
public const string RelativePath = "api/"; | ||
public const string GetPath = RelativePath + "people/"; | ||
public const string GetMiddlePath = RelativePath + "person/*/select"; | ||
|
||
public static List<Person> People = new List<Person> | ||
{ | ||
|
@@ -29,6 +30,31 @@ public class Person | |
new Person() {Key = 3, Name = "Luis Gonzalez", Age = 29, EmailAddress = "[email protected]"}, | ||
}; | ||
|
||
[WebApiHandler(HttpVerbs.Get, "/" + GetMiddlePath)] | ||
public bool GetPerson(WebServer server, HttpListenerContext context) | ||
{ | ||
try | ||
{ | ||
// read the middle segment | ||
var segment = context.Request.Url.Segments.Reverse().Skip(1).First().Replace("/", ""); | ||
|
||
// otherwise, we need to parse the key and respond with the entity accordingly | ||
int key; | ||
|
||
if (int.TryParse(segment, out key) && People.Any(p => p.Key == key)) | ||
{ | ||
return context.JsonResponse(People.FirstOrDefault(p => p.Key == key)); | ||
} | ||
|
||
throw new KeyNotFoundException("Key Not Found: " + segment); | ||
} | ||
catch (Exception ex) | ||
{ | ||
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; | ||
return context.JsonResponse(ex); | ||
} | ||
} | ||
|
||
[WebApiHandler(HttpVerbs.Get, "/" + GetPath + "*")] | ||
public bool GetPeople(WebServer server, HttpListenerContext context) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters