我在名为 SecureAPI 的控制器中有此方法。该路线看起来很简单,但无论我做什么,我似乎都无法调用它。
方法:
[Route("SecureAPI/SetOrderAsProcessedAsync/{uid}")]
public async Task<IActionResult> SetOrderAsProcessedAsync([FromRoute(Name = "uid")] Guid uid)
{
Order order = await _exp.Order(uid);
_db.Update(order);
order.IsProcessed = true;
order.DateProcessed = DateTime.Now;
await _db.SaveChangesAsync();
return RedirectToActionPermanent("Orders", "Secure");
}
链接:
<a asp-controller="SecureAPI" asp-action="SetOrderAsProcessedAsync" asp-all-route-data="@(new { uid = Model.Order.UId }.ToRouteData())" class="btn-antan action flex-center-center"><icon awesome="Lock" /> Set as processed</a>
生成此链接:
http://localhost:5294/SecureAPI/SetOrderAsProcessedAsync?uid=f345f4d2-b184-4652-bd1b-78cc2f28aaff
ToRouteData 扩展:
public static IDictionary<string, string> ToRouteData(this object obj)
{
IDictionary<string, string> result = new Dictionary<string, string>();
foreach (IObjectPropertyModel prop in obj.GetProperties())
{
result.Add(prop.Name, prop.Value);
}
return result;
}
有什么想法吗?谢谢!
asp-route-{value}
将该值解释为路由参数。
asp-route-uid="@Model.Order.UId"
<a asp-controller="SecureAPI" asp-action="SetOrderAsProcessedAsync" asp-route-uid="@Model.Order.UId" class="btn-antan action flex-center-center"><icon awesome="Lock" /> Set as processed</a>