Dave New
Dave New

Reputation: 40092

MVC 6 Web Api: Resolving the location header on a 201 (Created)

In Web Api 2.2, we could return the location header URL by returning from controller as follows:

return Created(new Uri(Url.Link("GetClient", new { id = clientId })), clientReponseModel);

Url.Link(..) would resolve the resource URL accordingly based on the controller name GetClient:

Location header

In ASP.NET 5 MVC 6's Web Api, Url doesn't exist within the framework but the CreatedResult constructor does have the location parameter:

return new CreatedResult("http://www.myapi.com/api/clients/" + clientId, journeyModel);

How can I resolve this URL this without having to manually supply it, like we did in Web API 2.2?

Upvotes: 18

Views: 18504

Answers (6)

Shimmy Weitzhandler
Shimmy Weitzhandler

Reputation: 104801

There is also CreatedAtRoute:

public async Task<IActionResult> PostImpl([FromBody] Entity entity)
{
  ...

  return CreatedAtRoute(entity.Id, entity);
  //or
  return CreatedAtRoute(new { id = entity.Id }, entity);      
}

Upvotes: 1

Dave New
Dave New

Reputation: 40092

I didn't realise it, but the CreatedAtAction() method caters for this:

return CreatedAtAction("GetClient", new { id = clientId }, clientReponseModel);

Ensure that your controller derives from MVC's Controller.

Upvotes: 21

Shimmy Weitzhandler
Shimmy Weitzhandler

Reputation: 104801

In the new ASP.NET MVC Core there is a property Url, which returns an instance of IUrlHelper. You can use it to generate a local URL by using the following:

[HttpPost]
public async Task<IActionResult> Post([FromBody] Person person)
{
  _DbContext.People.Add(person);
  await _DbContext.SaveChangesAsync();

  return Created(Url.RouteUrl(person.Id), person.Id);
}

Upvotes: 12

Julio Fernandez
Julio Fernandez

Reputation: 35

I use this simple approximation based on the Uri being served at the web server:

[HttpPost]
[Route("")]
public IHttpActionResult AddIntervencion(MyNS.MyType myObject) {
  return Created<MyNS.MyType>(Request.RequestUri + "/" + myObject.key, myObject);
}

Upvotes: -2

mcbowes
mcbowes

Reputation: 798

My GET action has a route name

    [HttpGet("{id:int}", Name = "GetOrganizationGroupByIdRoute")]
    public async Task<IActionResult> Get(int id, CancellationToken cancellationToken = default(CancellationToken))
    {
        ...
    }

And my POST action uses that route name to return the URL

    [HttpPost]
    public async Task<HttpStatusCodeResult> Post([FromBody]OrganizationGroupInput input, CancellationToken cancellationToken = default(CancellationToken))
    {
        ...
        var url = Url.RouteUrl("GetOrganizationGroupByIdRoute", new { id = item.Id }, Request.Scheme, Request.Host.ToUriComponent());
        Context.Response.Headers["Location"] = url;
        ...
    }

Resulting response using Fiddler enter image description here

Hope that helps.

Upvotes: 0

mbudnik
mbudnik

Reputation: 2107

There is an UrlHelper class which implements IUrlHelper interface. It provides the requested functionality.

Source code

Upvotes: 1

Related Questions