1
鑑於下面的OData自定義操作。是否有可能獲得更多的重構友好綁定的動作參數?強類型ODataActionParameters可能嗎?
這兩個魔法字符串必須完全相同:.Parameter<int>("Rating")
和(int)parameters["Rating"]
。未來的某個時間點必然會突破。
配置
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Product>("Products");
// New code:
builder.Namespace = "ProductService";
builder.EntityType<Product>()
.Action("Rate")
.Parameter<int>("Rating");
控制器
[HttpPost]
public async Task<IHttpActionResult> Rate([FromODataUri] int key, ODataActionParameters parameters)
{
if (!ModelState.IsValid)
{
return BadRequest();
}
int rating = (int)parameters["Rating"];
db.Ratings.Add(new ProductRating
{
ProductID = key,
Rating = rating
});
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateException e)
{
if (!ProductExists(key))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
請求
POST http://localhost/Products(1)/ProductService.Rate HTTP/1.1
Content-Type: application/json
Content-Length: 12
{"Rating":5}
我試圖把帕拉姆直接在方法中。但我無法實現它的工作。
public async Task<IHttpActionResult> Rate([FromODataUri] int key, int rate)
根據How to pass an objet as a parameter to an OData Action using ASP.NET Web Api?它似乎有可能使用Object
,但我只有一個基本類型作爲參數。
請指點:)
這似乎是相關的。猜猜我必須等待更新。 –