UriTemplateWCF

发布于 2024-11-09 05:44:55 字数 162 浏览 0 评论 0原文

有没有一种简单的方法可以在同一定义中拥有多个 UriTemplate。

 [WebGet(UriTemplate = "{id}")]

例如,我希望 /API/{id} 和 /API/{id}/ 调用相同的东西。我不希望最后是否有 / 重要。

Is there a simple way to have multiple UriTemplates in the same definition.

 [WebGet(UriTemplate = "{id}")]

For example I want /API/{id} and /API/{id}/ to call the same thing. I don't want it to matter if there is / at the end or not.

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

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

发布评论

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

评论(3

看春风乍起 2024-11-16 05:44:55

并不是很简单,但您可以在行为中使用操作选择器来去除尾随的“/”,如下例所示。

public class StackOverflow_6073581_751090
{
    [ServiceContract]
    public interface ITest
    {
        [WebGet(UriTemplate = "/API/{id}")]
        string Get(string id);
    }
    public class Service : ITest
    {
        public string Get(string id)
        {
            return id;
        }
    }
    public class MyBehavior : WebHttpBehavior
    {
        protected override WebHttpDispatchOperationSelector GetOperationSelector(ServiceEndpoint endpoint)
        {
            return new MySelector(endpoint);
        }

        class MySelector : WebHttpDispatchOperationSelector
        {
            public MySelector(ServiceEndpoint endpoint) : base(endpoint) { }

            protected override string SelectOperation(ref Message message, out bool uriMatched)
            {
                string result = base.SelectOperation(ref message, out uriMatched);
                if (!uriMatched)
                {
                    string address = message.Headers.To.AbsoluteUri;
                    if (address.EndsWith("/"))
                    {
                        message.Headers.To = new Uri(address.Substring(0, address.Length - 1));
                    }

                    result = base.SelectOperation(ref message, out uriMatched);
                }

                return result;
            }
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(ITest), new WebHttpBinding(), "").Behaviors.Add(new MyBehavior());
        host.Open();
        Console.WriteLine("Host opened");

        WebClient c = new WebClient();
        Console.WriteLine(c.DownloadString(baseAddress + "/API/2"));
        Console.WriteLine(c.DownloadString(baseAddress + "/API/2/"));

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}

Not really simple, but you can use an operation selector in your behavior to strip the trailing '/', like in the example below.

public class StackOverflow_6073581_751090
{
    [ServiceContract]
    public interface ITest
    {
        [WebGet(UriTemplate = "/API/{id}")]
        string Get(string id);
    }
    public class Service : ITest
    {
        public string Get(string id)
        {
            return id;
        }
    }
    public class MyBehavior : WebHttpBehavior
    {
        protected override WebHttpDispatchOperationSelector GetOperationSelector(ServiceEndpoint endpoint)
        {
            return new MySelector(endpoint);
        }

        class MySelector : WebHttpDispatchOperationSelector
        {
            public MySelector(ServiceEndpoint endpoint) : base(endpoint) { }

            protected override string SelectOperation(ref Message message, out bool uriMatched)
            {
                string result = base.SelectOperation(ref message, out uriMatched);
                if (!uriMatched)
                {
                    string address = message.Headers.To.AbsoluteUri;
                    if (address.EndsWith("/"))
                    {
                        message.Headers.To = new Uri(address.Substring(0, address.Length - 1));
                    }

                    result = base.SelectOperation(ref message, out uriMatched);
                }

                return result;
            }
        }
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(ITest), new WebHttpBinding(), "").Behaviors.Add(new MyBehavior());
        host.Open();
        Console.WriteLine("Host opened");

        WebClient c = new WebClient();
        Console.WriteLine(c.DownloadString(baseAddress + "/API/2"));
        Console.WriteLine(c.DownloadString(baseAddress + "/API/2/"));

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}
自在安然 2024-11-16 05:44:55

这只是部分有用,但新的 WCF Web API 库在 HttpBehavior 上有一个名为 TrailingSlashMode 的属性,可以设置该属性忽略或重定向。

This is only partially helpful, but the new WCF Web API library has a property on the HttpBehavior called TrailingSlashMode that can be set to Ignore or Redirect.

够钟 2024-11-16 05:44:55

我发现执行此操作的最简单方法是重载函数 如此处所述

The easiest way I found to do this is to overload the function as explained here.

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