【问题标题】:WebInvoke with UriTemplate with empty strings带有空字符串的 UriTemplate 的 WebInvoke
【发布时间】:2011-06-07 15:43:21
【问题描述】:

在运行时在占位符中提供空字符串时,WebInvokeAttribute 和 UriTemplate 解析器的行为如何?

documentation 似乎没有涵盖这一点。

在某些继承的代码中,我遇到了在传递空字符串时无法正确解析方法的情况。与其他web方法没有明显冲突。

谢谢!

更新:

在 UriTemplate 中,例如:"/{x}/{y}?z={z}",如果部分或全部值作为 "" 空字符串提供,但分隔符仍然存在,"/17/?z=""//apple?z=""//?z=%20"、@ 987654326@。此外,按照标准,浏览器是否允许在发送 URI 之前清理

【问题讨论】:

    标签: asp.net wcf webmethod uritemplate


    【解决方案1】:

    空字符串表示操作的 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();
        }
    }
    

    【讨论】:

    • 整个 uri 不是空字符串,运行时为空字符串值。
    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 2011-12-15
    • 2010-11-14
    • 2011-11-28
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多