DrewB
DrewB

Reputation: 1536

MVC 6 Web API generating urls

I am writing an app in MVC 6 and attempting to make a web api call. The url for the call will be http://mysite/api/Organizations/{id}/groups. How do I generate that URL in Razor? Normally, I use Url.Action("Get", "Organizations") but I'm not sure how to get the trailing /groups on it like that.

Upvotes: 0

Views: 33

Answers (1)

Shyju
Shyju

Reputation: 218892

Assuming you have defined this specific route pattern on one of your api controllers with a Route name

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() });
    }
}

You can use the Url.RouteUrl helper method to generate the Url (with the pattern we defined) by passing in the route name

<a href="@Url.RouteUrl("OrgGroupsRoute",
                                   new { httproute=true,id = 34})">Get Groups for Org </a>

Upvotes: 1

Related Questions