C# WebAPI 2 OData-没有针对'~/entityset/key/$links/navigation';
我对OData和WebAPI 2还很陌生。提前道歉 我的目标是使用一个OData服务,它有一个C# WebAPI 2 OData-没有针对'~/entityset/key/$links/navigation';,c#,asp.net,web-services,wcf,asp.net-web-api,C#,Asp.net,Web Services,Wcf,Asp.net Web Api,我对OData和WebAPI 2还很陌生。提前道歉 我的目标是使用一个OData服务,它有一个WcfService对象和一个WcfClient对象,并通过一个列表属性Clients将WcfClient与WcfService关联。(例如,service.Clients.Add(client)但使用OData服务) 我有一个EF code first数据库,其中包含WcfService、WcfClient和SoaApplication类。以及使用Microsoft.AspNet.WebApi.ODa
WcfService
对象和一个WcfClient
对象,并通过一个列表属性Clients
将WcfClient
与WcfService
关联。(例如,service.Clients.Add(client)
但使用OData服务)
我有一个EF code first数据库,其中包含WcfService
、WcfClient
和SoaApplication
类。以及使用Microsoft.AspNet.WebApi.OData 5.1.1的OData webservice设置。链接所有内容的代码都是使用VS2013中的向导设置的。OData端点的$metadata如下所示(http://soa.local.xxx/odata/$metadata
):
我用于添加对象之间关系的代码如下所示:
[DataServiceKey("ServiceId")]
public class WcfService
{
[Key]
public int ServiceId { get; set; }
... snip ...
public List<WcfClient> Clients { get; set; }
}
var ctx = new Container(uri);
var service = ctx.WcfServices.Expand("Clients".Where(i => i.ServiceId == 21).ToArray()[0];
var client = ctx.WcfClients.Where(i => i.ClientId == 220).ToArray()[0];
ctx.AddLink(service, "Clients", client);
ctx.SaveChanges();
不幸的是,出现了一个错误。这是通过网络(从fiddler)发送的完整请求和响应(为名称空间编辑)
请求:
POST http://soa.local.xxx/odata/WcfService(21)/$links/Clients HTTP/1.1
DataServiceVersion: 1.0;NetFx
MaxDataServiceVersion: 3.0;NetFx
Accept: application/atom+xml,application/xml
Accept-Charset: UTF-8
Content-Type: application/xml
User-Agent: Microsoft ADO.NET Data Services
Host: soa.local.xxx
Content-Length: 174
Expect: 100-continue
Connection: Keep-Alive
<?xml version="1.0" encoding="utf-8"?>
<uri xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">http://soa.local.xxx/odata/WcfClient(220)</uri>
POSThttp://soa.local.xxx/odata/WcfService(21)/$links/Clients HTTP/1.1
DataServiceVersion:1.0;NetFx
MaxDataServiceVersion:3.0;NetFx
接受:application/atom+xml,application/xml
接受字符集:UTF-8
内容类型:application/xml
用户代理:Microsoft ADO.NET数据服务
主机:soa.local.xxx
内容长度:174
预期:100人继续
连接:保持活力
http://soa.local.xxx/odata/WcfClient(220)
答复:
HTTP/1.1 404 Not Found
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/xml; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
DataServiceVersion: 3.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sat, 15 Mar 2014 23:50:12 GMT
Content-Length: 565
<?xml version="1.0" encoding="utf-8"?>
<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<m:code />
<m:message xml:lang="en-US">No HTTP resource was found that matches the request URI 'http://soa.local.xxx/odata/WcfService(21)/$links/Clients'.</m:message>
<m:innererror>
<m:message>No routing convention was found to select an action for the OData path with template '~/entityset/key/$links/navigation'.</m:message>
<m:type></m:type>
<m:stacktrace></m:stacktrace>
</m:innererror>
</m:error>
未找到HTTP/1.1 404
缓存控制:没有缓存
Pragma:没有缓存
内容类型:application/xml;字符集=utf-8
过期:-1
服务器:Microsoft IIS/7.5
DataServiceVersion:3.0
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
日期:2014年3月15日星期六23:50:12 GMT
内容长度:565
未找到与请求URI名称匹配的HTTP资源http://soa.local.xxx/odata/WcfService(21)/$links/客户'。
找不到路由约定,无法使用模板“~/entityset/key/$links/navigation”为OData路径选择操作。
我不明白为什么请求url的格式是'~/entityset(key)/$links/navigation'
,错误消息是'~/entityset/key/$links/navigation'
url中使用的格式似乎在中列出为受支持的格式。但是,我可能在错误的领域寻找解决方案
我应用变更的方式是否有问题?有没有更合适的方法来编写代码
谢谢看来我需要更彻底地阅读文档。其中有
CreateLink
和DeleteLink
示例方法。一旦这些方法被添加到控制器中,调用就开始成功进行
我猜路由约定已经设置好了;但是出现错误消息,因为没有将约定映射到的方法
HTTP/1.1 404 Not Found
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/xml; charset=utf-8
Expires: -1
Server: Microsoft-IIS/7.5
DataServiceVersion: 3.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sat, 15 Mar 2014 23:50:12 GMT
Content-Length: 565
<?xml version="1.0" encoding="utf-8"?>
<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<m:code />
<m:message xml:lang="en-US">No HTTP resource was found that matches the request URI 'http://soa.local.xxx/odata/WcfService(21)/$links/Clients'.</m:message>
<m:innererror>
<m:message>No routing convention was found to select an action for the OData path with template '~/entityset/key/$links/navigation'.</m:message>
<m:type></m:type>
<m:stacktrace></m:stacktrace>
</m:innererror>
</m:error>