簡體   English   中英

C#REST API,URL參數為空

[英]C# REST API, URL Parameter is null

我正在為我們的應用程序構建一個REST API。 到目前為止,基礎知識已經奏效。 我可以連接到該服務,它將調用命令。 但是,在我的一種方法中,有一個名為(字符串)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);

這是我從Web瀏覽器中的應用程序獲得的輸出:

<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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM