Jezz Santos - 445 - May 9, 2014

Is there any handy support built in for easily setting the Location header with a URL of the created resource after calling a POST?

I mean any handy methods, or some kind of pattern to calculate the routes and base URL from the code within the Service class, without having to hardcode that stuff?

Jezz Santos:

Thanks. But what are my options if I already return a response DTO? 

You mean like without modifying your Service? You can set the Location header yourself using a [AddHeader] attributes, or a response filter, ServiceRunner etc.

You can also do:
return HttpResult.Redirect(newUrl);

Jezz Santos:

Oh! oops, might have found a previous answer. Is this still the recommended way to do it?
https://groups.google.com/d/msg/servicestack/87n_-N2EYnk/aNO0JtZ13rwJ

Hardy Erlinger:

Try this:
return new HttpResult(HttpStatusCode.Created, “Created successfully.”)
{
     Location = new MyDto {Id = request.Id }.ToUrl(“GET”)
};

Jezz Santos:

Happy to change my service, but I am confused about what you mean here. My understanding of SS is that I can return either HttpResult or my own cutom DTO. So far in my services, I have been returning custom DTO’s, but the above answers indicate returning a HttpResult. Have I missed something more fundamental here? Can do both or something?

You can return a decorated HttpResult which is what we were suggesting with our HttpResult answers, e.g: https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack/HttpResult.cs#L322

But I assumed you didn’t want to do this when you asked:

> But what are my options if I already return a response DTO

Which is why I then suggested the other alternatives to modifying the HTTP Response whilst returning a response DTO.

Jezz Santos:

OK, no probs then.
Sorry, might have wasted peoples time on this one, appologies