Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Nancy route patterns to a single action?

Tags:

nancy

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 => 
{
   ...
};
like image 322
warrickh Avatar asked Feb 13 '23 19:02

warrickh


1 Answers

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;
        }
    }
like image 170
Fran Hoey Avatar answered Feb 27 '23 21:02

Fran Hoey