I try to make a class with NancyModules and GET string on URL but method 'Get' tells that:
"Error CS0021 Cannot apply indexing with [] to an expression of type 'method group' ...."
My Code:
using System; using System.Collections.Generic; using System.Linq;
using System.Web; using Nancy; using System.Text;
namespace SinglePageApp1 {
public class BloodPresureNancy : NancyModule
{
public BloodPresureNancy()
{
// Get dasn't work
Get["/"] = _ => "Heloooo";
}
}
}
I add references: Nancy, Nancy.Hosting.asp and it isn't working.
What version of Nancy are you currently using? That syntax should work on versions 1.x. However, I think that Nancy recently made a change to the way that you register routes for their upcoming 2.0 release. If you take a look at the samples they have on github https://github.com/NancyFx/Nancy/blob/master/samples/Nancy.Demo.Hosting.Self/TestModule.cs. You will see that you don't index into the different verbs anymore like you are doing above, you actually reference them like you would a Method. Try changing your code to be
Get("/", _ => { //do work here });instead and see if that works for you.
See https://github.com/NancyFx/Nancy/pull/2441
Specifically:
the wiki will not be updates as the 2.0 packages comes out of pre-release, until then all changes are considered as being pending =)
Ie. The magic custom indexer syntax that allowed you to do this:
Get["/"] = ...
Is gone in the Nancy 2.x releases.
However, all the documentation currently still refers to the current (ie. 1.4.x versions); so...
tldr; That syntax is old and gone in the new version of Nancy. Use Get(...)
, Post(...)
, etc if you're using the new version of Nancy.
While this is most likely not the right way of doing it, it does work:
Get("/test/{category}", parameters => { return "My category is " + (Nancy.DynamicDictionaryValue)parameters.category; });
Going to http://localhost/test/hello will return "My category is hello"
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