我正在用MVC 6编写一个应用程序,并试图调用web。调用的url将是http://mysite/api/Organizations/{id}/groups。如何在Razor中生成该URL?通常情况下,我使用Url.Action("Get", "Organizations"),但我不知道如何在它上得到尾随的/groups。
发布于 2016-02-10 21:43:16
假设您已经在一个api控制器上使用一个路由名称定义了这个特定的路由模式
public class OrganizationsController : ApiController
{
[Route("api/Organizations/{id}/groups", Name = "OrgGroupsRoute", Order = 1)]
public IHttpActionResult GetGroupsForOrg(int id)
{
return Ok(new string[] { "groups for org", id.ToString() });
}
}您可以使用Url.RouteUrl助手方法通过传入路由名称来生成Url (与我们定义的模式)
<a href="@Url.RouteUrl("OrgGroupsRoute",
new { httproute=true,id = 34})">Get Groups for Org </a>https://stackoverflow.com/questions/35326255
复制相似问题