Beatsleigher Beatsleigher - 1 year ago 140
reST (reStructuredText) Question

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:

[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:

<Value xmlns:a="">a:InternalServiceFault</Value>
<Text xml:lang="en-US">Value cannot be null. Parameter name: luceneQuery</Text>
<ExceptionDetail xmlns="" xmlns:i="">
<HelpLink i:nil="true"/>
<InnerException i:nil="true"/>
<Message>Value cannot be null. Parameter name: luceneQuery</Message>
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

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

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download