带有空字符串的 UriTemplate 的 WebInvoke

发布于 2024-11-14 10:36:01 字数 505 浏览 4 评论 0原文

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

文档似乎没有涵盖这一点。

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

谢谢!

更新:

在 UriTemplate 中,例如:"/{x}/{y}?z={z}",如果部分或全部值作为 "" 空字符串提供,会出现什么行为,但分隔符仍然存在,"/17/?z=", "//apple?z=", "//?z=%20" , <代码>“//?z =”。另外,按照标准,浏览器是否允许在发送 URI 之前清理 URI?

How does the WebInvokeAttribute and UriTemplate resolver behave when supplied with empty strings in placeholders at runtime?

The documentation doesn't seem to cover this.

In some inherited code, I'm getting situations where the methods are not being resolved properly when empty strings are passed. There is no obvious conflict with other web methods.

Thanks!

Update:

In a UriTemplate such as: "/{x}/{y}?z={z}", what is the behavior if some or all of the values are provided as "" empty strings, but the delimiters remain, "/17/?z=", "//apple?z=", "//?z=%20", "//?z=". Also, by standard, are browsers allowed to clean up URIs before sending them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑶笙 2024-11-21 10:36:01

空字符串意味着操作的 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();
    }
}

The empty string means that the URI for the operation is located at the same address as the endpoint - see the example below for more information.

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文