As you can see at this link, it was possible to separate the rooting and the implementation of the old nancy1.X modules. Now that the way of defining these routes have changed, I would like to know how to code the same separation logic.
For clarity, the old way to define a Get
route was:
Get["/{category}"] = parameters => "My category is " + parameters.category;
and the new way is:
Get("/{category}", args => "My category is " + args.category);
The old way to separate interface from implementation was:
Get["/favoriteNumber/{value:int}"] = FavoriteNumber;
private dynamic FavoriteNumber (dynamic parameters)
{
return "So your favorite number is " + parameters.value + "?";
}
and the new way is:
???
After experimenting with endpoint definitions, you may use either a Func<dynamic,object>
type specification (#0
below) or a wrapper for a method (#1
). That's because there are several overloads of Get
, so compiler requires these hints to choose proper one. Alternatively if method returns task it can be specified without wrapping (##2
,3
), e.g. async methods always return task:
Get("/favoriteNumber0/{value:int}", (Func<dynamic, object>)FavoriteNumberObject);
Get("/favoriteNumber1/{value:int}", arg => FavoriteNumber(arg));
Get("/favoriteNumber2/{value:int}", FavoriteNumberTask);
Get("/favoriteNumber3/{value:int}", FavoriteNumberTaskCt);
...
private object FavoriteNumberObject(dynamic parameters)
{
return "So your favorite number is " + parameters.value + "?";
}
private string FavoriteNumber(dynamic parameters)
{
return "So your favorite number is " + parameters.value + "?";
}
private Task<string> FavoriteNumberTask(dynamic parameters)
{
return Task.FromResult("So your favorite number is " + parameters["value"] + "?");
}
private Task<string> FavoriteNumberTaskCt(dynamic parameters, CancellationToken ctx)
{
return Task.FromResult("So your favorite number is " + parameters["value"] + "?");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With