【发布时间】:2014-05-02 01:03:51
【问题描述】:
我正在尝试将 WCF/REST 服务项目转换为 MVC/WebAPI。服务层作为不同终端系统的包装器被多次实现,所有实现都遵循接口中定义的通用契约 (IContract)。使用 WCF,我们在作为 Web 服务方法公开的每个方法上定义了 [WebInvoke] 和 [OperationContract] 属性。在 WebAPI 中,这可以通过在控制器上定义的属性路由来简化。但是,我想保留接口上定义的路由属性,因此所有实现的行为都相似。
这是旧界面的示例:
[ServiceContract]
public interface IContract
{
[WebInvoke(UriTemplate = "Version", Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
[OperationContract]
string GetVersion();
}
这是我希望得到的工作:
public interface IContract
{
[Route("Version")]
[HttpGet]
string GetVersion();
}
我也会考虑创建一个抽象基类,但是另一个StackOverflow question 让我觉得没有合适的替代品来替代[WebInvoke(UriTemplate)] 使用WebAPI 属性路由。是这样吗,或者有人可以向我指出类似的、受支持的技术吗?
谢谢
【问题讨论】:
标签: c# asp.net-mvc wcf asp.net-web-api