1

実行時にプレースホルダーに空の文字列が指定された場合、WebInvokeAttribute および UriTemplate リゾルバーはどのように動作しますか?

ドキュメントはこれをカバーしていないようです。

一部の継承されたコードでは、空の文字列が渡されたときにメソッドが適切に解決されない状況が発生しています。他の Web メソッドとの明らかな競合はありません。

ありがとう!

アップデート:

次のような UriTemplate"/{x}/{y}?z={z}"では、値の一部またはすべてが "" 空の文字列として指定されているが、区切り文字が残っている場合の動作はどうなりますか"/17/?z=", "//apple?z=", . また、標準では、ブラウザは送信前に URIをクリーンアップできますか?"//?z=%20""//?z="

4

1 に答える 1

-2

空の文字列は、操作の URI がエンドポイントと同じアドレスにあることを意味します。詳細については、以下の例を参照してください。

public class StackOverflow_6267866
{
    [ServiceContract]
    public interface ITest1
    {
        [WebInvoke(UriTemplate = "")]
        string EchoString(string text);
    }
    [ServiceContract]
    public interface ITest2
    {
        [WebInvoke(UriTemplate = "", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        int Add(int x, int y);
    }
    public class Service : ITest1, ITest2
    {
        public string EchoString(string text)
        {
            return text;
        }

        public int Add(int x, int y)
        {
            return x + y;
        }
    }
    static void SendPostRequest(string uri, string contentType, string body)
    {
        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
        req.Method = "POST";
        req.ContentType = contentType;
        byte[] bodyBytes = Encoding.UTF8.GetBytes(body);
        req.GetRequestStream().Write(bodyBytes, 0, bodyBytes.Length);
        req.GetRequestStream().Close();

        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        Console.WriteLine("HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription);
        foreach (string headerName in resp.Headers.AllKeys)
        {
            Console.WriteLine("{0}: {1}", headerName, resp.Headers[headerName]);
        }
        Console.WriteLine();
        Stream respStream = resp.GetResponseStream();
        Console.WriteLine(new StreamReader(respStream).ReadToEnd());
        Console.WriteLine();
        Console.WriteLine("  *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*  ");
        Console.WriteLine();
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(ITest1), new WebHttpBinding(), "ITest1").Behaviors.Add(new WebHttpBehavior());
        host.AddServiceEndpoint(typeof(ITest2), new WebHttpBinding(), "ITest2").Behaviors.Add(new WebHttpBehavior());
        host.Open();
        Console.WriteLine("Host opened");

        SendPostRequest(baseAddress + "/ITest1", "application/json", "\"hello world\"");
        SendPostRequest(baseAddress + "/ITest1/", "application/json", "\"hello world\"");
        SendPostRequest(baseAddress + "/ITest2", "application/json", "{\"x\":123,\"y\":456}");
        SendPostRequest(baseAddress + "/ITest2/", "application/json", "{\"x\":123,\"y\":456}");

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}
于 2011-06-07T16:48:18.873 に答える