【问题标题】:Restful webservice routing宁静的网络服务路由
【发布时间】:2011-08-31 15:30:04
【问题描述】:

我正在开发一个 .net 4.0 wcf restful 服务项目。作为项目的一部分,我创建了两个服务 1) OrderService 2)ProductService

此时我已在 Global.asax 中对它们进行如下配置:

RouteTable.Routes.Add(new ServiceRoute("products", new WebServiceHostFactory(),
                                                   typeof (ProductService)));
RouteTable.Routes.Add(new ServiceRoute("orders", new WebServiceHostFactory(),
                                                   typeof (OrderService)));

我可以使用以下网址访问服务:

    http://localhost/orders/123
    http://localhost/products/456

但我的要求是我必须能够使用以下格式的 url 以特定顺序访问特定产品:

   http://localhost/orders/{orderId}/products/{productId}

谁能建议我应该使用什么路由来让两个不同的服务协同工作。

更新:ProductService中有一个方法接受两个参数

  1. orderId 和

  2. 产品编号

    退回想要的产品

【问题讨论】:

    标签: c# wcf rest .net-4.0


    【解决方案1】:

    在您的OrderService(和相关服务合同)中,使用正确的UriTemplate 公开操作

    public class OrderService : IOrderService
    {
        [WebGet(UriTemplate = "{orderId}/products/{productId}")]
        public Product GetOrderProduct(int orderId, int productId)
        {
            ...
        }
    
    }
    

    如果它必须可以通过 orders 路由到 OrderService 访问,则不能在 ProductService 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-25
      • 2012-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多