It's a matter of calling the common logic. No magic I'm afraid. Note that Nancy's content negotiation can take care of returning a view or json based on the request.
Multiple Nancy route patterns to a single action?
-
11-10-2022 - |
문제
I would like to have the same action be performed by 2 or more route patterns.
For example:
//Route 1:
Get["/{category}/{product_name}/{id}"]
// Route 2:
Get["/api/products/{id}"]
Ideally the first route would be SEO friendly and return a view, the second route would return JSON and be used as an API.
Is it simply a matter of defining 2 separate routes and calling the common logic encapsulated in another method? Or is there some Nancy magic I don't know about?
Update My final solution was to use multiple assignments in the one statement.
Get["/{category}/{product_name}/{id}"] = Get["/api/products/{id}"] = params =>
{
...
};
해결책 2
다른 팁
I know this is answered, but I thought I'd add my tuppence for a slightly neater solution. This is my solution:
public class ExampleModule : NancyModule
{
public ExampleModule()
{
Get["/somepath"] = DoSomething;
Post["/somepath"] = DoSomething;
}
private dynamic DoSomething(dynamic parameters)
{
return null;
}
}
제휴하지 않습니다 StackOverflow