C# REST API, URL 参数为空
C# REST API, URL Parameter is null
我正在为我们的应用程序构建 REST API。到目前为止,基础工作。
我可以连接到服务,它会调用命令。
但是,在我的一个方法中,有一个名为 (string) luceneQuery 的参数,无论我将方法参数放在哪里,或者我在 URL 参数中键入什么,它始终为 null 或空。
老实说,我搞不懂这个,我已经试了几个小时了;甚至 Google 也让我望而却步。
这是我正在使用的路线:
public const string SearchRoute = "/search/{domain}/{query}/{outputType}";
调用的方法如下:
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);
}
该方法是从接口实现的:
[OperationContract]
[WebGet(UriTemplate = Routing.SearchRoute, BodyStyle = WebMessageBodyStyle.Bare)]
string SearchIndex(string domain, string luceneQuery, OutputType outputType);
这是我在网络浏览器中从应用程序获得的输出:
<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>
在此先感谢您能为我提供的任何帮助。
您的路由和方法签名似乎不匹配。您的路线定义 query
但您的方法签名有一个 luceneQuery
参数。尝试将您的方法更改为:
public string SearchIndex(string domain, string query, OutputType outputType)
我正在为我们的应用程序构建 REST API。到目前为止,基础工作。 我可以连接到服务,它会调用命令。 但是,在我的一个方法中,有一个名为 (string) luceneQuery 的参数,无论我将方法参数放在哪里,或者我在 URL 参数中键入什么,它始终为 null 或空。
老实说,我搞不懂这个,我已经试了几个小时了;甚至 Google 也让我望而却步。
这是我正在使用的路线:
public const string SearchRoute = "/search/{domain}/{query}/{outputType}";
调用的方法如下:
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);
}
该方法是从接口实现的:
[OperationContract]
[WebGet(UriTemplate = Routing.SearchRoute, BodyStyle = WebMessageBodyStyle.Bare)]
string SearchIndex(string domain, string luceneQuery, OutputType outputType);
这是我在网络浏览器中从应用程序获得的输出:
<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>
在此先感谢您能为我提供的任何帮助。
您的路由和方法签名似乎不匹配。您的路线定义 query
但您的方法签名有一个 luceneQuery
参数。尝试将您的方法更改为:
public string SearchIndex(string domain, string query, OutputType outputType)