我正在尝试模仿 NuGet.org 的行为,它为示例 URL 返回包 ID,例如:
http://www.nuget.org/api/v2/Packages(Id='Nuget.Core',Version='2.8.3')/Id
此响应如下所示:
<d:Id xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
Nuget.Core
</d:Id>
我可以使用PropertyRoutingConvention创建一个类似的 WebApi OData 路由并在我的控制器上创建一个方法:
public IHttpActionResult GetId([FromODataUri] string id, [FromODataUri] string version)
{
var package = Repository.FindPackage(id, new SemanticVersion(version));
return Ok(package.Id);
}
调用此操作,但响应始终具有 application/json 的 Content-Type:
curl -i -q -H 'Accept: application/atom+xml' http://localhost:9001/api/odata/Packages(Id='Nuget.Core',Version='2.8.3')/Id
HTTP/1.1 200 OK
Content-Type: application/json; odata=minimalmetadata; streaming=true; charset=utf-8
DataServiceVersion: 3.0
{
"odata.metadata":"http://localhost:9001/api/odata/$metadata#Edm.String","value":"Nuget.Core"
}
除了内容协商不起作用之外,如何在响应中包含属性名称以使其行为类似于 NuGet.org 的行为?
为了清楚起见,完整的代码可在https://github.com/themotleyfool/NuGet.Lucene