Question

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 => 
{
   ...
};
Was it helpful?

Solution 2

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.

OTHER TIPS

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;
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top