简体   繁体   English

RESTful WCF Web服务的UriTemplate前缀

[英]UriTemplate prefix for RESTful WCF web service

I have various interfaces (endpoints) in a WCF service host, each for a completely different concern. 我在WCF服务主机中有各种接口(端点),每个接口都有完全不同的关注点。 In a classic soapy web service, I'm able to define a base host address (eg http://myhost.com/ ) and map each interface to a relative URI ( IServiceContract -> service/ , IMaintenanceContract -> maintenance/ ) so I can call them by eg http://myhost.com/service/mymethod . 在经典的肥皂网络服务中,我能够定义基本主机地址(例如http://myhost.com/ )并将每个接口映射到相对URI( IServiceContract - > service/IMaintenanceContract - > maintenance/ )所以我可以通过例如http://myhost.com/service/mymethod给他们打电话。

Now I'm taking my first steps towards a RESTful WCF service using JSON as message format for CRUD web requests and the only thing I see to address an operation is by using the UriTemplate field from WebInvoke (or WebGet ) attribute. 现在,我正在迈出RESTful WCF服务的第一步,使用JSON作为CRUD Web请求的消息格式,我唯一看到的解决操作的方法是使用WebInvoke (或WebGet )属性中的UriTemplate字段。 Unfortunately, it doesn't seem that I can put this on the interface, just on operation contract methods. 不幸的是,似乎我不能把它放在接口上,只是在操作合同方法上。

How can I map each interface to a different relative URI ? 如何将每个接口映射到不同的相对URI

Yes, you'll put the base url on the [OperationContract] methods. 是的,您将基本URL放在[OperationContract]方法上。 This is OK though, because you can specify any base url you want. 这没关系,因为您可以指定所需的任何基本URL。 Here is a sample interface that gives you this control. 这是一个示例界面,为您提供此控件。

namespace MyHostApi
{
    [ServiceContract]
    public interface IMyHostApi
    {

        [OperationContract]
        [WebGet(BodyStyle = WebMessageBodyStyle.Bare,
                UriTemplate = "WhateverYouWant/HelloWorld/{name}")]
        string HelloWorld(string format, string name);

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM