Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

linq

get

nancy

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

enter image description here

I add references: Nancy, Nancy.Hosting.asp and it isn't working.

like image 655
Bartek Popielarczyk Avatar asked Sep 19 '16 13:09

Bartek Popielarczyk


3 Answers

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.
like image 56
Komeisa Avatar answered Nov 15 '22 14:11

Komeisa


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.

like image 29
Doug Avatar answered Nov 15 '22 14:11

Doug


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"

like image 44
Mark Phillips Avatar answered Nov 15 '22 15:11

Mark Phillips