Cannot apply indexing with [] to an expression of type "method group" SinglePageApp1. Get ["/"] Nancy

I am trying to create a class with NancyModules and a GET string at the URL, but the 'Get' method reports that:

"Error CS0021 Unable to apply indexing with [] to an expression of type 'method group' ...."

My code is:

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"; } } } 

enter image description here

I add links: Nancy, Nancy.Hosting.asp and does not work.

+5
source share
3 answers

What version of Nancy are you currently using? This syntax should work on versions 1.x. However, I think that Nancy has recently made changes to the route registration method for their upcoming version 2.0. If you 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 are no longer indexing different verbs, as you do above, you are actually referring to them as a method. Try changing the code instead

  Get ("/", _ => {
         // do work here
      });
and see if this works for you.
+7
source

Although this is most likely not the right way to do this, it works:

 Get("/test/{category}", parameters => { return "My category is " + (Nancy.DynamicDictionaryValue)parameters.category; }); 

Going to http: // localhost / test / hello will return "My category welcomes"

+1
source

See https://github.com/NancyFx/Nancy/pull/2441

In particular:

the wiki will not be updated as 2.0 packages are released from the preliminary release, until then all changes are considered pending =)

Those. The magic custom indexer syntax that allowed you to do this:

 Get["/"] = ... 

Left in version Nancy 2.x.

However, all the documentation currently still refers to the current version (i.e. version 1.4.x); so that...

TL; DR; This syntax is old and gone into the new version of Nancy. Use Get(...) , Post(...) etc. if you are using the new version of Nancy.

+1
source

All Articles