2

How can I use ActionLink when calling an action on a controller that is a WebApi controller.

public class RequestController : ApiController
{

    [ActionName("CreateAction")]
    [ResponseType(typeof(Request))]
    public async Task<IHttpActionResult> PostRequest(Request request)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.Requests.Add(request);
        await db.SaveChangesAsync();

        return CreatedAtRoute("DefaultApi", new { id = request.Id }, request);
    }

}

in my layout page can I do this

  • @Html.ActionLink("Request", "CreateAction", "api/Request", new { area = "" }, null)
  • Jawahar

    Jawahar
    • 183
    • 4
    • 16

    1 Answers1

    2

    You can generate a link to an API controller using the standard Url helper property. The following link will point to an Albums controller and pass along an id parameter with a value of 3:

    @Url.RouteUrl("DefaultApi", new { httproute=true, controller="Albums", id=3})
    

    Please refer to this link http://odetocode.com/blogs/scott/archive/2013/03/27/webapi-tip-5-generating-links.aspx

    Abdisamad Khalif
    • 765
    • 2
    • 7
    • 19