SimonC
SimonC

Reputation: 1615

C# REST API, URL Parameter is null

I'm building a REST API for our application. So far, the basics work. I can connect to the service, and it will invoke commands. However, in one of my methods, there is a parameter called (string) luceneQuery, and no matter where I put the method parameter, or what I type in the URL parameter, it is always null or empty.

I honestly can't figure this one out, and I've been having a go at it for a couple of hours; even Google has been putting me off.

Here is the route I'm using:

public const string SearchRoute = "/search/{domain}/{query}/{outputType}";

The method that is invoked is as follows:

public string SearchIndex(string domain, string luceneQuery, OutputType outputType) {
        if (string.IsNullOrEmpty(domain))
            throw new ArgumentNullException(nameof(domain));
        else if (string.IsNullOrEmpty(luceneQuery))
            throw new ArgumentNullException(nameof(luceneQuery));

        var byteArray = Convert.FromBase64String(luceneQuery);
        luceneQuery = Encoding.UTF8.GetString(byteArray);

        return ExecuteCommand("search", outputType == OutputType.Json ? "-json" : "", "--default", domain, luceneQuery);
    }

The method is implemented from an interface:

[OperationContract]
    [WebGet(UriTemplate = Routing.SearchRoute, BodyStyle = WebMessageBodyStyle.Bare)]
    string SearchIndex(string domain, string luceneQuery, OutputType outputType);

And this the output I get from the application in my web browser:

<Code>
    <Value xmlns:a="http://schemas.microsoft.com/ws/2005/05/addressing/none">a:InternalServiceFault</Value>
</Code>
<Reason>
    <Text xml:lang="en-US">Value cannot be null. Parameter name: luceneQuery</Text>
</Reason>
<Detail>
    <ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
        <HelpLink i:nil="true"/>
        <InnerException i:nil="true"/>
        <Message>Value cannot be null. Parameter name: luceneQuery</Message>
        <StackTrace>
            at De.Nwt.MailArchive.RestService.RestService.SearchIndex (System.String domain, System.String luceneQuery, De.Nwt.MailArchive.RestService.OutputType outputType) [0x00027] in /Users/simoncahill/Projects/MailArchive/RestService/Src/Classes/RestService.cs:104 at (wrapper managed-to-native)                 System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&) at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00038] in /private/tmp/source-mono-4.6.0/bockbuild-xamarin/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/corlib/System.Reflection/MonoMethod.cs:305
        </StackTrace>
        <Type>System.ArgumentNullException</Type>
    </ExceptionDetail>
</Detail>

Thanks in advance for any help you can provide me with.

Upvotes: 0

Views: 954

Answers (1)

Stavros Kasidis
Stavros Kasidis

Reputation: 48

It seems that your route and your method signature do not match. Your route defines query but your method signature has a luceneQuery parameter. Try changing your method to:

public string SearchIndex(string domain, string query, OutputType outputType)

Upvotes: 1

Related Questions