سؤال

I was trying to access dynamic property in Nancy. In Nancy if pass parameter in query it comes as dynamic property. How can I access that.

There are many discussion / question about this but every where, first it is of creating dynamic and then consuming it. How can I consume what is already created?

Here are two code snippet

public class ParameterModule : NancyModule
    {
        public ParameterModule():base("/{about}")
        {
            this.Get["/"] = (parameters) => "Hello About" + parameters.about;
        }
    }

and for F#

type ParameterModule() as this = 
    inherit NancyModule("/{about}")
    do this.Get.["/"] <- fun parameters -> "Hello" + parameters?("about") :> obj

I can't access about as object don't have that property.

Please let me know if any further information needed.

هل كانت مفيدة؟

المحلول

The F# dynamic operator (?) allows you to pass string parameters without using the quotes, achieving a similar syntax to C# dynamic, but you need to define it first for your concrete use case, the compiler just provides the syntax. Try this:

let (?) (parameters:obj) param =
    (parameters :?> Nancy.DynamicDictionary).[param]

type ParameterModule() as this = 
    inherit NancyModule("/{about}")
    do this.Get.["/"] <- fun parameters -> sprintf "Hello %O" parameters?about :> obj

نصائح أخرى

I have solved problem by typecasting to dynamicdictionary. If there is any better way please let me know. Will keep the question open till then...

Here is the code that solve the issue

type ParameterModule() as this = 
    inherit NancyModule("/{about}")
    do this.Get.["/"] <- fun parameters -> (parameters :?> Nancy.DynamicDictionary).["about"].ToString() :> obj

Maybe this can get you started http://hubfs.net/topic/None/74053

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top