0
我試圖張貼對象OData的動作,在這裏我的代碼的OData 4個Web API 2 「沒有路由慣例發現」
public class DraftController : ODataController
{
public HttpResponseMessage Attachment([FromODataUri] string key, [FromBody] DraftApi d)
{
try
{
return Request.CreateResponse(HttpStatusCode.Created, "(POST ATTACHMENT) key: " + key + " - id: " + d.id + ", desc: " + d.description);
}
catch (Exception e)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, e.Message);
}
}
}
這是我的模型
public class DraftApi
{
[Key]
public string id { get; set; }
public string description { get; set; }
}
這是我的OData的路線
config.MapODataServiceRoute(
routeName: "ODataDraft",
routePrefix: "odata/{namespace}",
model: BuildModel<DraftApi>("draft")
);
private static IEdmModel BuildModel<T>(string EntityName) where T : class
{
ODataConventionModelBuilder ODataBuilder = new ODataConventionModelBuilder();
ODataBuilder.EntitySet<T>(EntityName).EntityType.Name = EntityName;
ActionConfiguration attachment = ODataBuilder.EntityType<T>().Action("Attachment");
ODataBuilder.EnableLowerCamelCase();
return ODataBuilder.GetEdmModel();
}
我的電話是這樣的
Url
http://127.0.0.1/mpssapi/odata/v1/draft('hhh')/Attachment
Headers
Content-Type: application/json
Payload
{id:"a", description: "abc"}
這是我的迴應
{
"error": {
"code": ""
"message": "No HTTP resource was found that matches the request URI 'http://127.0.0.1/mpssapi/odata/v1/draft('hhh')/Attachment'."
"innererror": {
"message": "No routing convention was found to select an action for the OData path with template '~/entityset/key/unresolved'."
"type": ""
"stacktrace": ""
}-
}-
}
我試圖命名空間添加到OData的路線,但它不工作 任何想法? 感謝
太棒了!非常感謝 –